ASTContext.cpp revision 198398
1193326Sed//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements the ASTContext interface. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#include "clang/AST/ASTContext.h" 15193326Sed#include "clang/AST/DeclCXX.h" 16193326Sed#include "clang/AST/DeclObjC.h" 17193326Sed#include "clang/AST/DeclTemplate.h" 18198092Srdivacky#include "clang/AST/TypeLoc.h" 19193326Sed#include "clang/AST/Expr.h" 20193326Sed#include "clang/AST/ExternalASTSource.h" 21193326Sed#include "clang/AST/RecordLayout.h" 22194179Sed#include "clang/Basic/Builtins.h" 23193326Sed#include "clang/Basic/SourceManager.h" 24193326Sed#include "clang/Basic/TargetInfo.h" 25193326Sed#include "llvm/ADT/StringExtras.h" 26193326Sed#include "llvm/Support/MathExtras.h" 27193326Sed#include "llvm/Support/MemoryBuffer.h" 28198092Srdivacky#include "RecordLayoutBuilder.h" 29198092Srdivacky 30193326Sedusing namespace clang; 31193326Sed 32193326Sedenum FloatingRank { 33193326Sed FloatRank, DoubleRank, LongDoubleRank 34193326Sed}; 35193326Sed 36193326SedASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, 37193326Sed TargetInfo &t, 38193326Sed IdentifierTable &idents, SelectorTable &sels, 39194179Sed Builtin::Context &builtins, 40198092Srdivacky bool FreeMem, unsigned size_reserve) : 41198092Srdivacky GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), 42198092Srdivacky ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), 43198398Srdivacky sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), 44198398Srdivacky SourceMgr(SM), LangOpts(LOpts), 45198092Srdivacky LoadedExternalComments(false), FreeMemory(FreeMem), Target(t), 46195341Sed Idents(idents), Selectors(sels), 47198092Srdivacky BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) { 48198092Srdivacky ObjCIdRedefinitionType = QualType(); 49198092Srdivacky ObjCClassRedefinitionType = QualType(); 50198092Srdivacky if (size_reserve > 0) Types.reserve(size_reserve); 51198092Srdivacky TUDecl = TranslationUnitDecl::Create(*this); 52193326Sed InitBuiltinTypes(); 53193326Sed} 54193326Sed 55193326SedASTContext::~ASTContext() { 56193326Sed // Deallocate all the types. 57193326Sed while (!Types.empty()) { 58193326Sed Types.back()->Destroy(*this); 59193326Sed Types.pop_back(); 60193326Sed } 61193326Sed 62193326Sed { 63198092Srdivacky llvm::FoldingSet<ExtQuals>::iterator 64198092Srdivacky I = ExtQualNodes.begin(), E = ExtQualNodes.end(); 65198092Srdivacky while (I != E) 66198092Srdivacky Deallocate(&*I++); 67198092Srdivacky } 68198092Srdivacky 69198092Srdivacky { 70193326Sed llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator 71193326Sed I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); 72193326Sed while (I != E) { 73193326Sed ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); 74193326Sed delete R; 75193326Sed } 76193326Sed } 77193326Sed 78193326Sed { 79193326Sed llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator 80193326Sed I = ObjCLayouts.begin(), E = ObjCLayouts.end(); 81193326Sed while (I != E) { 82193326Sed ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); 83193326Sed delete R; 84193326Sed } 85193326Sed } 86193326Sed 87193326Sed // Destroy nested-name-specifiers. 88193326Sed for (llvm::FoldingSet<NestedNameSpecifier>::iterator 89193326Sed NNS = NestedNameSpecifiers.begin(), 90198092Srdivacky NNSEnd = NestedNameSpecifiers.end(); 91198092Srdivacky NNS != NNSEnd; 92193326Sed /* Increment in loop */) 93193326Sed (*NNS++).Destroy(*this); 94193326Sed 95193326Sed if (GlobalNestedNameSpecifier) 96193326Sed GlobalNestedNameSpecifier->Destroy(*this); 97193326Sed 98193326Sed TUDecl->Destroy(*this); 99193326Sed} 100193326Sed 101198092Srdivackyvoid 102193326SedASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { 103193326Sed ExternalSource.reset(Source.take()); 104193326Sed} 105193326Sed 106193326Sedvoid ASTContext::PrintStats() const { 107193326Sed fprintf(stderr, "*** AST Context Stats:\n"); 108193326Sed fprintf(stderr, " %d types total.\n", (int)Types.size()); 109193326Sed 110193326Sed unsigned counts[] = { 111198092Srdivacky#define TYPE(Name, Parent) 0, 112193326Sed#define ABSTRACT_TYPE(Name, Parent) 113193326Sed#include "clang/AST/TypeNodes.def" 114193326Sed 0 // Extra 115193326Sed }; 116193326Sed 117193326Sed for (unsigned i = 0, e = Types.size(); i != e; ++i) { 118193326Sed Type *T = Types[i]; 119193326Sed counts[(unsigned)T->getTypeClass()]++; 120193326Sed } 121193326Sed 122193326Sed unsigned Idx = 0; 123193326Sed unsigned TotalBytes = 0; 124193326Sed#define TYPE(Name, Parent) \ 125193326Sed if (counts[Idx]) \ 126193326Sed fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ 127193326Sed TotalBytes += counts[Idx] * sizeof(Name##Type); \ 128193326Sed ++Idx; 129193326Sed#define ABSTRACT_TYPE(Name, Parent) 130193326Sed#include "clang/AST/TypeNodes.def" 131198092Srdivacky 132193326Sed fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); 133193326Sed 134193326Sed if (ExternalSource.get()) { 135193326Sed fprintf(stderr, "\n"); 136193326Sed ExternalSource->PrintStats(); 137193326Sed } 138193326Sed} 139193326Sed 140193326Sed 141193326Sedvoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { 142198092Srdivacky BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); 143198092Srdivacky R = QualType(Ty, 0); 144198092Srdivacky Types.push_back(Ty); 145193326Sed} 146193326Sed 147193326Sedvoid ASTContext::InitBuiltinTypes() { 148193326Sed assert(VoidTy.isNull() && "Context reinitialized?"); 149198092Srdivacky 150193326Sed // C99 6.2.5p19. 151193326Sed InitBuiltinType(VoidTy, BuiltinType::Void); 152198092Srdivacky 153193326Sed // C99 6.2.5p2. 154193326Sed InitBuiltinType(BoolTy, BuiltinType::Bool); 155193326Sed // C99 6.2.5p3. 156193576Sed if (LangOpts.CharIsSigned) 157193326Sed InitBuiltinType(CharTy, BuiltinType::Char_S); 158193326Sed else 159193326Sed InitBuiltinType(CharTy, BuiltinType::Char_U); 160193326Sed // C99 6.2.5p4. 161193326Sed InitBuiltinType(SignedCharTy, BuiltinType::SChar); 162193326Sed InitBuiltinType(ShortTy, BuiltinType::Short); 163193326Sed InitBuiltinType(IntTy, BuiltinType::Int); 164193326Sed InitBuiltinType(LongTy, BuiltinType::Long); 165193326Sed InitBuiltinType(LongLongTy, BuiltinType::LongLong); 166198092Srdivacky 167193326Sed // C99 6.2.5p6. 168193326Sed InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); 169193326Sed InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); 170193326Sed InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); 171193326Sed InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); 172193326Sed InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); 173198092Srdivacky 174193326Sed // C99 6.2.5p10. 175193326Sed InitBuiltinType(FloatTy, BuiltinType::Float); 176193326Sed InitBuiltinType(DoubleTy, BuiltinType::Double); 177193326Sed InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); 178193326Sed 179193326Sed // GNU extension, 128-bit integers. 180193326Sed InitBuiltinType(Int128Ty, BuiltinType::Int128); 181193326Sed InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); 182193326Sed 183193326Sed if (LangOpts.CPlusPlus) // C++ 3.9.1p5 184193326Sed InitBuiltinType(WCharTy, BuiltinType::WChar); 185193326Sed else // C99 186193326Sed WCharTy = getFromTargetType(Target.getWCharType()); 187193326Sed 188198092Srdivacky if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ 189198092Srdivacky InitBuiltinType(Char16Ty, BuiltinType::Char16); 190198092Srdivacky else // C99 191198092Srdivacky Char16Ty = getFromTargetType(Target.getChar16Type()); 192198092Srdivacky 193198092Srdivacky if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ 194198092Srdivacky InitBuiltinType(Char32Ty, BuiltinType::Char32); 195198092Srdivacky else // C99 196198092Srdivacky Char32Ty = getFromTargetType(Target.getChar32Type()); 197198092Srdivacky 198193326Sed // Placeholder type for functions. 199193326Sed InitBuiltinType(OverloadTy, BuiltinType::Overload); 200193326Sed 201193326Sed // Placeholder type for type-dependent expressions whose type is 202193326Sed // completely unknown. No code should ever check a type against 203193326Sed // DependentTy and users should never see it; however, it is here to 204193326Sed // help diagnose failures to properly check for type-dependent 205193326Sed // expressions. 206193326Sed InitBuiltinType(DependentTy, BuiltinType::Dependent); 207193326Sed 208198092Srdivacky // Placeholder type for C++0x auto declarations whose real type has 209195099Sed // not yet been deduced. 210195099Sed InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); 211198092Srdivacky 212193326Sed // C99 6.2.5p11. 213193326Sed FloatComplexTy = getComplexType(FloatTy); 214193326Sed DoubleComplexTy = getComplexType(DoubleTy); 215193326Sed LongDoubleComplexTy = getComplexType(LongDoubleTy); 216193326Sed 217193326Sed BuiltinVaListType = QualType(); 218198092Srdivacky 219198092Srdivacky // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). 220198092Srdivacky ObjCIdTypedefType = QualType(); 221198092Srdivacky ObjCClassTypedefType = QualType(); 222198092Srdivacky 223198092Srdivacky // Builtin types for 'id' and 'Class'. 224198092Srdivacky InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); 225198092Srdivacky InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); 226198092Srdivacky 227193326Sed ObjCConstantStringType = QualType(); 228198092Srdivacky 229193326Sed // void * type 230193326Sed VoidPtrTy = getPointerType(VoidTy); 231193326Sed 232193326Sed // nullptr type (C++0x 2.14.7) 233193326Sed InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); 234193326Sed} 235193326Sed 236198092SrdivackyMemberSpecializationInfo * 237198112SrdivackyASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { 238198092Srdivacky assert(Var->isStaticDataMember() && "Not a static data member"); 239198112Srdivacky llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos 240198092Srdivacky = InstantiatedFromStaticDataMember.find(Var); 241198092Srdivacky if (Pos == InstantiatedFromStaticDataMember.end()) 242198092Srdivacky return 0; 243198092Srdivacky 244198092Srdivacky return Pos->second; 245198092Srdivacky} 246198092Srdivacky 247198092Srdivackyvoid 248198092SrdivackyASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, 249198092Srdivacky TemplateSpecializationKind TSK) { 250198092Srdivacky assert(Inst->isStaticDataMember() && "Not a static data member"); 251198092Srdivacky assert(Tmpl->isStaticDataMember() && "Not a static data member"); 252198092Srdivacky assert(!InstantiatedFromStaticDataMember[Inst] && 253198092Srdivacky "Already noted what static data member was instantiated from"); 254198092Srdivacky InstantiatedFromStaticDataMember[Inst] 255198092Srdivacky = new (*this) MemberSpecializationInfo(Tmpl, TSK); 256198092Srdivacky} 257198092Srdivacky 258198092SrdivackyUnresolvedUsingDecl * 259198092SrdivackyASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) { 260198092Srdivacky llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos 261198092Srdivacky = InstantiatedFromUnresolvedUsingDecl.find(UUD); 262198092Srdivacky if (Pos == InstantiatedFromUnresolvedUsingDecl.end()) 263198092Srdivacky return 0; 264198092Srdivacky 265198092Srdivacky return Pos->second; 266198092Srdivacky} 267198092Srdivacky 268198092Srdivackyvoid 269198092SrdivackyASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD, 270198092Srdivacky UnresolvedUsingDecl *UUD) { 271198092Srdivacky assert(!InstantiatedFromUnresolvedUsingDecl[UD] && 272198092Srdivacky "Already noted what using decl what instantiated from"); 273198092Srdivacky InstantiatedFromUnresolvedUsingDecl[UD] = UUD; 274198092Srdivacky} 275198092Srdivacky 276198092SrdivackyFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { 277198092Srdivacky llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos 278198092Srdivacky = InstantiatedFromUnnamedFieldDecl.find(Field); 279198092Srdivacky if (Pos == InstantiatedFromUnnamedFieldDecl.end()) 280198092Srdivacky return 0; 281198092Srdivacky 282198092Srdivacky return Pos->second; 283198092Srdivacky} 284198092Srdivacky 285198092Srdivackyvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, 286198092Srdivacky FieldDecl *Tmpl) { 287198092Srdivacky assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); 288198092Srdivacky assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); 289198092Srdivacky assert(!InstantiatedFromUnnamedFieldDecl[Inst] && 290198092Srdivacky "Already noted what unnamed field was instantiated from"); 291198092Srdivacky 292198092Srdivacky InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; 293198092Srdivacky} 294198092Srdivacky 295195341Sednamespace { 296198092Srdivacky class BeforeInTranslationUnit 297195341Sed : std::binary_function<SourceRange, SourceRange, bool> { 298195341Sed SourceManager *SourceMgr; 299198092Srdivacky 300195341Sed public: 301195341Sed explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } 302198092Srdivacky 303195341Sed bool operator()(SourceRange X, SourceRange Y) { 304195341Sed return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); 305195341Sed } 306195341Sed }; 307195341Sed} 308195341Sed 309195341Sed/// \brief Determine whether the given comment is a Doxygen-style comment. 310195341Sed/// 311195341Sed/// \param Start the start of the comment text. 312195341Sed/// 313195341Sed/// \param End the end of the comment text. 314195341Sed/// 315195341Sed/// \param Member whether we want to check whether this is a member comment 316195341Sed/// (which requires a < after the Doxygen-comment delimiter). Otherwise, 317195341Sed/// we only return true when we find a non-member comment. 318198092Srdivackystatic bool 319198092SrdivackyisDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, 320195341Sed bool Member = false) { 321198092Srdivacky const char *BufferStart 322195341Sed = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; 323195341Sed const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); 324195341Sed const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); 325198092Srdivacky 326195341Sed if (End - Start < 4) 327195341Sed return false; 328195341Sed 329195341Sed assert(Start[0] == '/' && "Not a comment?"); 330195341Sed if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) 331195341Sed return false; 332195341Sed if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) 333195341Sed return false; 334195341Sed 335195341Sed return (Start[3] == '<') == Member; 336195341Sed} 337195341Sed 338195341Sed/// \brief Retrieve the comment associated with the given declaration, if 339198092Srdivacky/// it has one. 340195341Sedconst char *ASTContext::getCommentForDecl(const Decl *D) { 341195341Sed if (!D) 342195341Sed return 0; 343198092Srdivacky 344195341Sed // Check whether we have cached a comment string for this declaration 345195341Sed // already. 346198092Srdivacky llvm::DenseMap<const Decl *, std::string>::iterator Pos 347195341Sed = DeclComments.find(D); 348195341Sed if (Pos != DeclComments.end()) 349195341Sed return Pos->second.c_str(); 350195341Sed 351198092Srdivacky // If we have an external AST source and have not yet loaded comments from 352195341Sed // that source, do so now. 353195341Sed if (ExternalSource && !LoadedExternalComments) { 354195341Sed std::vector<SourceRange> LoadedComments; 355195341Sed ExternalSource->ReadComments(LoadedComments); 356198092Srdivacky 357195341Sed if (!LoadedComments.empty()) 358195341Sed Comments.insert(Comments.begin(), LoadedComments.begin(), 359195341Sed LoadedComments.end()); 360198092Srdivacky 361195341Sed LoadedExternalComments = true; 362195341Sed } 363198092Srdivacky 364198092Srdivacky // If there are no comments anywhere, we won't find anything. 365195341Sed if (Comments.empty()) 366195341Sed return 0; 367195341Sed 368195341Sed // If the declaration doesn't map directly to a location in a file, we 369195341Sed // can't find the comment. 370195341Sed SourceLocation DeclStartLoc = D->getLocStart(); 371195341Sed if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) 372195341Sed return 0; 373195341Sed 374195341Sed // Find the comment that occurs just before this declaration. 375195341Sed std::vector<SourceRange>::iterator LastComment 376198092Srdivacky = std::lower_bound(Comments.begin(), Comments.end(), 377195341Sed SourceRange(DeclStartLoc), 378195341Sed BeforeInTranslationUnit(&SourceMgr)); 379198092Srdivacky 380195341Sed // Decompose the location for the start of the declaration and find the 381195341Sed // beginning of the file buffer. 382198092Srdivacky std::pair<FileID, unsigned> DeclStartDecomp 383195341Sed = SourceMgr.getDecomposedLoc(DeclStartLoc); 384198092Srdivacky const char *FileBufferStart 385195341Sed = SourceMgr.getBufferData(DeclStartDecomp.first).first; 386198092Srdivacky 387195341Sed // First check whether we have a comment for a member. 388195341Sed if (LastComment != Comments.end() && 389195341Sed !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && 390195341Sed isDoxygenComment(SourceMgr, *LastComment, true)) { 391195341Sed std::pair<FileID, unsigned> LastCommentEndDecomp 392195341Sed = SourceMgr.getDecomposedLoc(LastComment->getEnd()); 393195341Sed if (DeclStartDecomp.first == LastCommentEndDecomp.first && 394195341Sed SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) 395198092Srdivacky == SourceMgr.getLineNumber(LastCommentEndDecomp.first, 396195341Sed LastCommentEndDecomp.second)) { 397195341Sed // The Doxygen member comment comes after the declaration starts and 398195341Sed // is on the same line and in the same file as the declaration. This 399195341Sed // is the comment we want. 400195341Sed std::string &Result = DeclComments[D]; 401198092Srdivacky Result.append(FileBufferStart + 402198092Srdivacky SourceMgr.getFileOffset(LastComment->getBegin()), 403195341Sed FileBufferStart + LastCommentEndDecomp.second + 1); 404195341Sed return Result.c_str(); 405195341Sed } 406195341Sed } 407198092Srdivacky 408195341Sed if (LastComment == Comments.begin()) 409195341Sed return 0; 410195341Sed --LastComment; 411195341Sed 412195341Sed // Decompose the end of the comment. 413195341Sed std::pair<FileID, unsigned> LastCommentEndDecomp 414195341Sed = SourceMgr.getDecomposedLoc(LastComment->getEnd()); 415198092Srdivacky 416195341Sed // If the comment and the declaration aren't in the same file, then they 417195341Sed // aren't related. 418195341Sed if (DeclStartDecomp.first != LastCommentEndDecomp.first) 419195341Sed return 0; 420198092Srdivacky 421195341Sed // Check that we actually have a Doxygen comment. 422195341Sed if (!isDoxygenComment(SourceMgr, *LastComment)) 423195341Sed return 0; 424198092Srdivacky 425195341Sed // Compute the starting line for the declaration and for the end of the 426195341Sed // comment (this is expensive). 427198092Srdivacky unsigned DeclStartLine 428195341Sed = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); 429195341Sed unsigned CommentEndLine 430198092Srdivacky = SourceMgr.getLineNumber(LastCommentEndDecomp.first, 431195341Sed LastCommentEndDecomp.second); 432198092Srdivacky 433195341Sed // If the comment does not end on the line prior to the declaration, then 434195341Sed // the comment is not associated with the declaration at all. 435195341Sed if (CommentEndLine + 1 != DeclStartLine) 436195341Sed return 0; 437198092Srdivacky 438195341Sed // We have a comment, but there may be more comments on the previous lines. 439195341Sed // Keep looking so long as the comments are still Doxygen comments and are 440195341Sed // still adjacent. 441198092Srdivacky unsigned ExpectedLine 442195341Sed = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; 443195341Sed std::vector<SourceRange>::iterator FirstComment = LastComment; 444195341Sed while (FirstComment != Comments.begin()) { 445195341Sed // Look at the previous comment 446195341Sed --FirstComment; 447195341Sed std::pair<FileID, unsigned> Decomp 448195341Sed = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); 449198092Srdivacky 450195341Sed // If this previous comment is in a different file, we're done. 451195341Sed if (Decomp.first != DeclStartDecomp.first) { 452195341Sed ++FirstComment; 453195341Sed break; 454195341Sed } 455198092Srdivacky 456195341Sed // If this comment is not a Doxygen comment, we're done. 457195341Sed if (!isDoxygenComment(SourceMgr, *FirstComment)) { 458195341Sed ++FirstComment; 459195341Sed break; 460195341Sed } 461198092Srdivacky 462195341Sed // If the line number is not what we expected, we're done. 463195341Sed unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); 464195341Sed if (Line != ExpectedLine) { 465195341Sed ++FirstComment; 466195341Sed break; 467195341Sed } 468198092Srdivacky 469195341Sed // Set the next expected line number. 470198092Srdivacky ExpectedLine 471195341Sed = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; 472195341Sed } 473198092Srdivacky 474195341Sed // The iterator range [FirstComment, LastComment] contains all of the 475195341Sed // BCPL comments that, together, are associated with this declaration. 476195341Sed // Form a single comment block string for this declaration that concatenates 477195341Sed // all of these comments. 478195341Sed std::string &Result = DeclComments[D]; 479195341Sed while (FirstComment != LastComment) { 480195341Sed std::pair<FileID, unsigned> DecompStart 481195341Sed = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); 482195341Sed std::pair<FileID, unsigned> DecompEnd 483195341Sed = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); 484195341Sed Result.append(FileBufferStart + DecompStart.second, 485195341Sed FileBufferStart + DecompEnd.second + 1); 486195341Sed ++FirstComment; 487195341Sed } 488198092Srdivacky 489195341Sed // Append the last comment line. 490198092Srdivacky Result.append(FileBufferStart + 491198092Srdivacky SourceMgr.getFileOffset(LastComment->getBegin()), 492195341Sed FileBufferStart + LastCommentEndDecomp.second + 1); 493195341Sed return Result.c_str(); 494195341Sed} 495195341Sed 496193326Sed//===----------------------------------------------------------------------===// 497193326Sed// Type Sizing and Analysis 498193326Sed//===----------------------------------------------------------------------===// 499193326Sed 500193326Sed/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 501193326Sed/// scalar floating point type. 502193326Sedconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { 503198092Srdivacky const BuiltinType *BT = T->getAs<BuiltinType>(); 504193326Sed assert(BT && "Not a floating point type!"); 505193326Sed switch (BT->getKind()) { 506193326Sed default: assert(0 && "Not a floating point type!"); 507193326Sed case BuiltinType::Float: return Target.getFloatFormat(); 508193326Sed case BuiltinType::Double: return Target.getDoubleFormat(); 509193326Sed case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); 510193326Sed } 511193326Sed} 512193326Sed 513198092Srdivacky/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the 514193326Sed/// specified decl. Note that bitfields do not have a valid alignment, so 515193326Sed/// this method will assert on them. 516193326Sedunsigned ASTContext::getDeclAlignInBytes(const Decl *D) { 517193326Sed unsigned Align = Target.getCharWidth(); 518193326Sed 519195341Sed if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) 520193326Sed Align = std::max(Align, AA->getAlignment()); 521193326Sed 522193326Sed if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { 523193326Sed QualType T = VD->getType(); 524198092Srdivacky if (const ReferenceType* RT = T->getAs<ReferenceType>()) { 525193326Sed unsigned AS = RT->getPointeeType().getAddressSpace(); 526193326Sed Align = Target.getPointerAlign(AS); 527193326Sed } else if (!T->isIncompleteType() && !T->isFunctionType()) { 528193326Sed // Incomplete or function types default to 1. 529193326Sed while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) 530193326Sed T = cast<ArrayType>(T)->getElementType(); 531193326Sed 532193326Sed Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); 533193326Sed } 534193326Sed } 535193326Sed 536193326Sed return Align / Target.getCharWidth(); 537193326Sed} 538193326Sed 539193326Sed/// getTypeSize - Return the size of the specified type, in bits. This method 540193326Sed/// does not work on incomplete types. 541198092Srdivacky/// 542198092Srdivacky/// FIXME: Pointers into different addr spaces could have different sizes and 543198092Srdivacky/// alignment requirements: getPointerInfo should take an AddrSpace, this 544198092Srdivacky/// should take a QualType, &c. 545193326Sedstd::pair<uint64_t, unsigned> 546193326SedASTContext::getTypeInfo(const Type *T) { 547193326Sed uint64_t Width=0; 548193326Sed unsigned Align=8; 549193326Sed switch (T->getTypeClass()) { 550193326Sed#define TYPE(Class, Base) 551193326Sed#define ABSTRACT_TYPE(Class, Base) 552193326Sed#define NON_CANONICAL_TYPE(Class, Base) 553193326Sed#define DEPENDENT_TYPE(Class, Base) case Type::Class: 554193326Sed#include "clang/AST/TypeNodes.def" 555193326Sed assert(false && "Should not see dependent types"); 556193326Sed break; 557193326Sed 558193326Sed case Type::FunctionNoProto: 559193326Sed case Type::FunctionProto: 560193326Sed // GCC extension: alignof(function) = 32 bits 561193326Sed Width = 0; 562193326Sed Align = 32; 563193326Sed break; 564193326Sed 565193326Sed case Type::IncompleteArray: 566193326Sed case Type::VariableArray: 567193326Sed Width = 0; 568193326Sed Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); 569193326Sed break; 570193326Sed 571193326Sed case Type::ConstantArray: { 572193326Sed const ConstantArrayType *CAT = cast<ConstantArrayType>(T); 573198092Srdivacky 574193326Sed std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); 575193326Sed Width = EltInfo.first*CAT->getSize().getZExtValue(); 576193326Sed Align = EltInfo.second; 577193326Sed break; 578193326Sed } 579193326Sed case Type::ExtVector: 580193326Sed case Type::Vector: { 581198398Srdivacky const VectorType *VT = cast<VectorType>(T); 582198398Srdivacky std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); 583198398Srdivacky Width = EltInfo.first*VT->getNumElements(); 584193326Sed Align = Width; 585193326Sed // If the alignment is not a power of 2, round up to the next power of 2. 586193326Sed // This happens for non-power-of-2 length vectors. 587198398Srdivacky if (VT->getNumElements() & (VT->getNumElements()-1)) { 588198398Srdivacky Align = llvm::NextPowerOf2(Align); 589198398Srdivacky Width = llvm::RoundUpToAlignment(Width, Align); 590198398Srdivacky } 591193326Sed break; 592193326Sed } 593193326Sed 594193326Sed case Type::Builtin: 595193326Sed switch (cast<BuiltinType>(T)->getKind()) { 596193326Sed default: assert(0 && "Unknown builtin type!"); 597193326Sed case BuiltinType::Void: 598193326Sed // GCC extension: alignof(void) = 8 bits. 599193326Sed Width = 0; 600193326Sed Align = 8; 601193326Sed break; 602193326Sed 603193326Sed case BuiltinType::Bool: 604193326Sed Width = Target.getBoolWidth(); 605193326Sed Align = Target.getBoolAlign(); 606193326Sed break; 607193326Sed case BuiltinType::Char_S: 608193326Sed case BuiltinType::Char_U: 609193326Sed case BuiltinType::UChar: 610193326Sed case BuiltinType::SChar: 611193326Sed Width = Target.getCharWidth(); 612193326Sed Align = Target.getCharAlign(); 613193326Sed break; 614193326Sed case BuiltinType::WChar: 615193326Sed Width = Target.getWCharWidth(); 616193326Sed Align = Target.getWCharAlign(); 617193326Sed break; 618198092Srdivacky case BuiltinType::Char16: 619198092Srdivacky Width = Target.getChar16Width(); 620198092Srdivacky Align = Target.getChar16Align(); 621198092Srdivacky break; 622198092Srdivacky case BuiltinType::Char32: 623198092Srdivacky Width = Target.getChar32Width(); 624198092Srdivacky Align = Target.getChar32Align(); 625198092Srdivacky break; 626193326Sed case BuiltinType::UShort: 627193326Sed case BuiltinType::Short: 628193326Sed Width = Target.getShortWidth(); 629193326Sed Align = Target.getShortAlign(); 630193326Sed break; 631193326Sed case BuiltinType::UInt: 632193326Sed case BuiltinType::Int: 633193326Sed Width = Target.getIntWidth(); 634193326Sed Align = Target.getIntAlign(); 635193326Sed break; 636193326Sed case BuiltinType::ULong: 637193326Sed case BuiltinType::Long: 638193326Sed Width = Target.getLongWidth(); 639193326Sed Align = Target.getLongAlign(); 640193326Sed break; 641193326Sed case BuiltinType::ULongLong: 642193326Sed case BuiltinType::LongLong: 643193326Sed Width = Target.getLongLongWidth(); 644193326Sed Align = Target.getLongLongAlign(); 645193326Sed break; 646193326Sed case BuiltinType::Int128: 647193326Sed case BuiltinType::UInt128: 648193326Sed Width = 128; 649193326Sed Align = 128; // int128_t is 128-bit aligned on all targets. 650193326Sed break; 651193326Sed case BuiltinType::Float: 652193326Sed Width = Target.getFloatWidth(); 653193326Sed Align = Target.getFloatAlign(); 654193326Sed break; 655193326Sed case BuiltinType::Double: 656193326Sed Width = Target.getDoubleWidth(); 657193326Sed Align = Target.getDoubleAlign(); 658193326Sed break; 659193326Sed case BuiltinType::LongDouble: 660193326Sed Width = Target.getLongDoubleWidth(); 661193326Sed Align = Target.getLongDoubleAlign(); 662193326Sed break; 663193326Sed case BuiltinType::NullPtr: 664193326Sed Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) 665193326Sed Align = Target.getPointerAlign(0); // == sizeof(void*) 666193326Sed break; 667193326Sed } 668193326Sed break; 669193326Sed case Type::FixedWidthInt: 670193326Sed // FIXME: This isn't precisely correct; the width/alignment should depend 671193326Sed // on the available types for the target 672193326Sed Width = cast<FixedWidthIntType>(T)->getWidth(); 673193326Sed Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); 674193326Sed Align = Width; 675193326Sed break; 676194613Sed case Type::ObjCObjectPointer: 677193326Sed Width = Target.getPointerWidth(0); 678193326Sed Align = Target.getPointerAlign(0); 679193326Sed break; 680193326Sed case Type::BlockPointer: { 681193326Sed unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); 682193326Sed Width = Target.getPointerWidth(AS); 683193326Sed Align = Target.getPointerAlign(AS); 684193326Sed break; 685193326Sed } 686193326Sed case Type::Pointer: { 687193326Sed unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); 688193326Sed Width = Target.getPointerWidth(AS); 689193326Sed Align = Target.getPointerAlign(AS); 690193326Sed break; 691193326Sed } 692193326Sed case Type::LValueReference: 693193326Sed case Type::RValueReference: 694193326Sed // "When applied to a reference or a reference type, the result is the size 695193326Sed // of the referenced type." C++98 5.3.3p2: expr.sizeof. 696193326Sed // FIXME: This is wrong for struct layout: a reference in a struct has 697193326Sed // pointer size. 698193326Sed return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); 699193326Sed case Type::MemberPointer: { 700193326Sed // FIXME: This is ABI dependent. We use the Itanium C++ ABI. 701193326Sed // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers 702193326Sed // If we ever want to support other ABIs this needs to be abstracted. 703193326Sed 704193326Sed QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); 705198092Srdivacky std::pair<uint64_t, unsigned> PtrDiffInfo = 706193326Sed getTypeInfo(getPointerDiffType()); 707193326Sed Width = PtrDiffInfo.first; 708193326Sed if (Pointee->isFunctionType()) 709193326Sed Width *= 2; 710193326Sed Align = PtrDiffInfo.second; 711193326Sed break; 712193326Sed } 713193326Sed case Type::Complex: { 714193326Sed // Complex types have the same alignment as their elements, but twice the 715193326Sed // size. 716198092Srdivacky std::pair<uint64_t, unsigned> EltInfo = 717193326Sed getTypeInfo(cast<ComplexType>(T)->getElementType()); 718193326Sed Width = EltInfo.first*2; 719193326Sed Align = EltInfo.second; 720193326Sed break; 721193326Sed } 722193326Sed case Type::ObjCInterface: { 723193326Sed const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); 724193326Sed const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); 725193326Sed Width = Layout.getSize(); 726193326Sed Align = Layout.getAlignment(); 727193326Sed break; 728193326Sed } 729193326Sed case Type::Record: 730193326Sed case Type::Enum: { 731193326Sed const TagType *TT = cast<TagType>(T); 732193326Sed 733193326Sed if (TT->getDecl()->isInvalidDecl()) { 734193326Sed Width = 1; 735193326Sed Align = 1; 736193326Sed break; 737193326Sed } 738198092Srdivacky 739193326Sed if (const EnumType *ET = dyn_cast<EnumType>(TT)) 740193326Sed return getTypeInfo(ET->getDecl()->getIntegerType()); 741193326Sed 742193326Sed const RecordType *RT = cast<RecordType>(TT); 743193326Sed const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); 744193326Sed Width = Layout.getSize(); 745193326Sed Align = Layout.getAlignment(); 746193326Sed break; 747193326Sed } 748193326Sed 749198398Srdivacky case Type::SubstTemplateTypeParm: 750198398Srdivacky return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> 751198398Srdivacky getReplacementType().getTypePtr()); 752198092Srdivacky 753198398Srdivacky case Type::Elaborated: 754198398Srdivacky return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() 755198398Srdivacky .getTypePtr()); 756198398Srdivacky 757193326Sed case Type::Typedef: { 758193326Sed const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); 759195341Sed if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { 760193326Sed Align = Aligned->getAlignment(); 761193326Sed Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); 762193326Sed } else 763193326Sed return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); 764193326Sed break; 765193326Sed } 766193326Sed 767193326Sed case Type::TypeOfExpr: 768193326Sed return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() 769193326Sed .getTypePtr()); 770193326Sed 771193326Sed case Type::TypeOf: 772193326Sed return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); 773193326Sed 774195099Sed case Type::Decltype: 775195099Sed return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() 776195099Sed .getTypePtr()); 777195099Sed 778193326Sed case Type::QualifiedName: 779193326Sed return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); 780198092Srdivacky 781193326Sed case Type::TemplateSpecialization: 782198092Srdivacky assert(getCanonicalType(T) != T && 783193326Sed "Cannot request the size of a dependent type"); 784193326Sed // FIXME: this is likely to be wrong once we support template 785193326Sed // aliases, since a template alias could refer to a typedef that 786193326Sed // has an __aligned__ attribute on it. 787193326Sed return getTypeInfo(getCanonicalType(T)); 788193326Sed } 789198092Srdivacky 790193326Sed assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); 791193326Sed return std::make_pair(Width, Align); 792193326Sed} 793193326Sed 794193326Sed/// getPreferredTypeAlign - Return the "preferred" alignment of the specified 795193326Sed/// type for the current target in bits. This can be different than the ABI 796193326Sed/// alignment in cases where it is beneficial for performance to overalign 797193326Sed/// a data type. 798193326Sedunsigned ASTContext::getPreferredTypeAlign(const Type *T) { 799193326Sed unsigned ABIAlign = getTypeAlign(T); 800193326Sed 801193326Sed // Double and long long should be naturally aligned if possible. 802198092Srdivacky if (const ComplexType* CT = T->getAs<ComplexType>()) 803193326Sed T = CT->getElementType().getTypePtr(); 804193326Sed if (T->isSpecificBuiltinType(BuiltinType::Double) || 805193326Sed T->isSpecificBuiltinType(BuiltinType::LongLong)) 806193326Sed return std::max(ABIAlign, (unsigned)getTypeSize(T)); 807193326Sed 808193326Sed return ABIAlign; 809193326Sed} 810193326Sed 811193326Sedstatic void CollectLocalObjCIvars(ASTContext *Ctx, 812193326Sed const ObjCInterfaceDecl *OI, 813193326Sed llvm::SmallVectorImpl<FieldDecl*> &Fields) { 814193326Sed for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 815193326Sed E = OI->ivar_end(); I != E; ++I) { 816193326Sed ObjCIvarDecl *IVDecl = *I; 817193326Sed if (!IVDecl->isInvalidDecl()) 818193326Sed Fields.push_back(cast<FieldDecl>(IVDecl)); 819193326Sed } 820193326Sed} 821193326Sed 822193326Sedvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, 823193326Sed llvm::SmallVectorImpl<FieldDecl*> &Fields) { 824193326Sed if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) 825193326Sed CollectObjCIvars(SuperClass, Fields); 826193326Sed CollectLocalObjCIvars(this, OI, Fields); 827193326Sed} 828193326Sed 829193576Sed/// ShallowCollectObjCIvars - 830193576Sed/// Collect all ivars, including those synthesized, in the current class. 831193576Sed/// 832193576Sedvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 833193576Sed llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, 834193576Sed bool CollectSynthesized) { 835193576Sed for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 836193576Sed E = OI->ivar_end(); I != E; ++I) { 837193576Sed Ivars.push_back(*I); 838193576Sed } 839193576Sed if (CollectSynthesized) 840193576Sed CollectSynthesizedIvars(OI, Ivars); 841193576Sed} 842193576Sed 843193326Sedvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 844193326Sed llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { 845195341Sed for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), 846195341Sed E = PD->prop_end(); I != E; ++I) 847193326Sed if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) 848193326Sed Ivars.push_back(Ivar); 849198092Srdivacky 850193326Sed // Also look into nested protocols. 851193326Sed for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), 852193326Sed E = PD->protocol_end(); P != E; ++P) 853193326Sed CollectProtocolSynthesizedIvars(*P, Ivars); 854193326Sed} 855193326Sed 856193326Sed/// CollectSynthesizedIvars - 857193326Sed/// This routine collect synthesized ivars for the designated class. 858193326Sed/// 859193326Sedvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, 860193326Sed llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { 861195341Sed for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), 862195341Sed E = OI->prop_end(); I != E; ++I) { 863193326Sed if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) 864193326Sed Ivars.push_back(Ivar); 865193326Sed } 866193326Sed // Also look into interface's protocol list for properties declared 867193326Sed // in the protocol and whose ivars are synthesized. 868193326Sed for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), 869193326Sed PE = OI->protocol_end(); P != PE; ++P) { 870193326Sed ObjCProtocolDecl *PD = (*P); 871193326Sed CollectProtocolSynthesizedIvars(PD, Ivars); 872193326Sed } 873193326Sed} 874193326Sed 875193576Sedunsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { 876193576Sed unsigned count = 0; 877195341Sed for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), 878195341Sed E = PD->prop_end(); I != E; ++I) 879193576Sed if ((*I)->getPropertyIvarDecl()) 880193576Sed ++count; 881193576Sed 882193576Sed // Also look into nested protocols. 883193576Sed for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), 884193576Sed E = PD->protocol_end(); P != E; ++P) 885193576Sed count += CountProtocolSynthesizedIvars(*P); 886193576Sed return count; 887193576Sed} 888193576Sed 889198092Srdivackyunsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) { 890193576Sed unsigned count = 0; 891195341Sed for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), 892195341Sed E = OI->prop_end(); I != E; ++I) { 893193576Sed if ((*I)->getPropertyIvarDecl()) 894193576Sed ++count; 895193576Sed } 896193576Sed // Also look into interface's protocol list for properties declared 897193576Sed // in the protocol and whose ivars are synthesized. 898193576Sed for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), 899193576Sed PE = OI->protocol_end(); P != PE; ++P) { 900193576Sed ObjCProtocolDecl *PD = (*P); 901193576Sed count += CountProtocolSynthesizedIvars(PD); 902193576Sed } 903193576Sed return count; 904193576Sed} 905193576Sed 906198092Srdivacky/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 907198092SrdivackyObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { 908198092Srdivacky llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator 909198092Srdivacky I = ObjCImpls.find(D); 910198092Srdivacky if (I != ObjCImpls.end()) 911198092Srdivacky return cast<ObjCImplementationDecl>(I->second); 912198092Srdivacky return 0; 913198092Srdivacky} 914198092Srdivacky/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 915198092SrdivackyObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { 916198092Srdivacky llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator 917198092Srdivacky I = ObjCImpls.find(D); 918198092Srdivacky if (I != ObjCImpls.end()) 919198092Srdivacky return cast<ObjCCategoryImplDecl>(I->second); 920198092Srdivacky return 0; 921198092Srdivacky} 922198092Srdivacky 923198092Srdivacky/// \brief Set the implementation of ObjCInterfaceDecl. 924198092Srdivackyvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, 925198092Srdivacky ObjCImplementationDecl *ImplD) { 926198092Srdivacky assert(IFaceD && ImplD && "Passed null params"); 927198092Srdivacky ObjCImpls[IFaceD] = ImplD; 928198092Srdivacky} 929198092Srdivacky/// \brief Set the implementation of ObjCCategoryDecl. 930198092Srdivackyvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, 931198092Srdivacky ObjCCategoryImplDecl *ImplD) { 932198092Srdivacky assert(CatD && ImplD && "Passed null params"); 933198092Srdivacky ObjCImpls[CatD] = ImplD; 934198092Srdivacky} 935198092Srdivacky 936198092Srdivacky/// \brief Allocate an uninitialized DeclaratorInfo. 937198092Srdivacky/// 938198092Srdivacky/// The caller should initialize the memory held by DeclaratorInfo using 939198092Srdivacky/// the TypeLoc wrappers. 940198092Srdivacky/// 941198092Srdivacky/// \param T the type that will be the basis for type source info. This type 942198092Srdivacky/// should refer to how the declarator was written in source code, not to 943198092Srdivacky/// what type semantic analysis resolved the declarator to. 944198398SrdivackyDeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T, 945198398Srdivacky unsigned DataSize) { 946198398Srdivacky if (!DataSize) 947198398Srdivacky DataSize = TypeLoc::getFullDataSizeForType(T); 948198398Srdivacky else 949198398Srdivacky assert(DataSize == TypeLoc::getFullDataSizeForType(T) && 950198398Srdivacky "incorrect data size provided to CreateDeclaratorInfo!"); 951198398Srdivacky 952198092Srdivacky DeclaratorInfo *DInfo = 953198092Srdivacky (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8); 954198092Srdivacky new (DInfo) DeclaratorInfo(T); 955198092Srdivacky return DInfo; 956198092Srdivacky} 957198092Srdivacky 958193326Sed/// getInterfaceLayoutImpl - Get or compute information about the 959193326Sed/// layout of the given interface. 960193326Sed/// 961193326Sed/// \param Impl - If given, also include the layout of the interface's 962193326Sed/// implementation. This may differ by including synthesized ivars. 963193326Sedconst ASTRecordLayout & 964193326SedASTContext::getObjCLayout(const ObjCInterfaceDecl *D, 965193326Sed const ObjCImplementationDecl *Impl) { 966193326Sed assert(!D->isForwardDecl() && "Invalid interface decl!"); 967193326Sed 968193326Sed // Look up this layout, if already laid out, return what we have. 969198092Srdivacky ObjCContainerDecl *Key = 970193326Sed Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; 971193326Sed if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) 972193326Sed return *Entry; 973193326Sed 974193326Sed // Add in synthesized ivar count if laying out an implementation. 975193326Sed if (Impl) { 976198092Srdivacky unsigned FieldCount = D->ivar_size(); 977193576Sed unsigned SynthCount = CountSynthesizedIvars(D); 978193576Sed FieldCount += SynthCount; 979193326Sed // If there aren't any sythesized ivars then reuse the interface 980193326Sed // entry. Note we can't cache this because we simply free all 981193326Sed // entries later; however we shouldn't look up implementations 982193326Sed // frequently. 983193576Sed if (SynthCount == 0) 984193326Sed return getObjCLayout(D, 0); 985193326Sed } 986193326Sed 987198092Srdivacky const ASTRecordLayout *NewEntry = 988198092Srdivacky ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); 989198092Srdivacky ObjCLayouts[Key] = NewEntry; 990193326Sed 991193326Sed return *NewEntry; 992193326Sed} 993193326Sed 994193326Sedconst ASTRecordLayout & 995193326SedASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { 996193326Sed return getObjCLayout(D, 0); 997193326Sed} 998193326Sed 999193326Sedconst ASTRecordLayout & 1000193326SedASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { 1001193326Sed return getObjCLayout(D->getClassInterface(), D); 1002193326Sed} 1003193326Sed 1004193326Sed/// getASTRecordLayout - Get or compute information about the layout of the 1005193326Sed/// specified record (struct/union/class), which indicates its size and field 1006193326Sed/// position information. 1007193326Sedconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { 1008193326Sed D = D->getDefinition(*this); 1009193326Sed assert(D && "Cannot get layout of forward declarations!"); 1010193326Sed 1011193326Sed // Look up this layout, if already laid out, return what we have. 1012198092Srdivacky // Note that we can't save a reference to the entry because this function 1013198092Srdivacky // is recursive. 1014198092Srdivacky const ASTRecordLayout *Entry = ASTRecordLayouts[D]; 1015193326Sed if (Entry) return *Entry; 1016193326Sed 1017198092Srdivacky const ASTRecordLayout *NewEntry = 1018198092Srdivacky ASTRecordLayoutBuilder::ComputeLayout(*this, D); 1019198092Srdivacky ASTRecordLayouts[D] = NewEntry; 1020193326Sed 1021193326Sed return *NewEntry; 1022193326Sed} 1023193326Sed 1024193326Sed//===----------------------------------------------------------------------===// 1025193326Sed// Type creation/memoization methods 1026193326Sed//===----------------------------------------------------------------------===// 1027193326Sed 1028198092SrdivackyQualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { 1029198092Srdivacky unsigned Fast = Quals.getFastQualifiers(); 1030198092Srdivacky Quals.removeFastQualifiers(); 1031198092Srdivacky 1032198092Srdivacky // Check if we've already instantiated this type. 1033198092Srdivacky llvm::FoldingSetNodeID ID; 1034198092Srdivacky ExtQuals::Profile(ID, TypeNode, Quals); 1035198092Srdivacky void *InsertPos = 0; 1036198092Srdivacky if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { 1037198092Srdivacky assert(EQ->getQualifiers() == Quals); 1038198092Srdivacky QualType T = QualType(EQ, Fast); 1039198092Srdivacky return T; 1040198092Srdivacky } 1041198092Srdivacky 1042198092Srdivacky ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); 1043198092Srdivacky ExtQualNodes.InsertNode(New, InsertPos); 1044198092Srdivacky QualType T = QualType(New, Fast); 1045198092Srdivacky return T; 1046198092Srdivacky} 1047198092Srdivacky 1048198092SrdivackyQualType ASTContext::getVolatileType(QualType T) { 1049198092Srdivacky QualType CanT = getCanonicalType(T); 1050198092Srdivacky if (CanT.isVolatileQualified()) return T; 1051198092Srdivacky 1052198092Srdivacky QualifierCollector Quals; 1053198092Srdivacky const Type *TypeNode = Quals.strip(T); 1054198092Srdivacky Quals.addVolatile(); 1055198092Srdivacky 1056198092Srdivacky return getExtQualType(TypeNode, Quals); 1057198092Srdivacky} 1058198092Srdivacky 1059193326SedQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { 1060193326Sed QualType CanT = getCanonicalType(T); 1061193326Sed if (CanT.getAddressSpace() == AddressSpace) 1062193326Sed return T; 1063193326Sed 1064198092Srdivacky // If we are composing extended qualifiers together, merge together 1065198092Srdivacky // into one ExtQuals node. 1066198092Srdivacky QualifierCollector Quals; 1067198092Srdivacky const Type *TypeNode = Quals.strip(T); 1068193326Sed 1069198092Srdivacky // If this type already has an address space specified, it cannot get 1070198092Srdivacky // another one. 1071198092Srdivacky assert(!Quals.hasAddressSpace() && 1072198092Srdivacky "Type cannot be in multiple addr spaces!"); 1073198092Srdivacky Quals.addAddressSpace(AddressSpace); 1074198092Srdivacky 1075198092Srdivacky return getExtQualType(TypeNode, Quals); 1076193326Sed} 1077193326Sed 1078193326SedQualType ASTContext::getObjCGCQualType(QualType T, 1079198092Srdivacky Qualifiers::GC GCAttr) { 1080193326Sed QualType CanT = getCanonicalType(T); 1081193326Sed if (CanT.getObjCGCAttr() == GCAttr) 1082193326Sed return T; 1083198092Srdivacky 1084193401Sed if (T->isPointerType()) { 1085198092Srdivacky QualType Pointee = T->getAs<PointerType>()->getPointeeType(); 1086198092Srdivacky if (Pointee->isAnyPointerType()) { 1087193401Sed QualType ResultType = getObjCGCQualType(Pointee, GCAttr); 1088193401Sed return getPointerType(ResultType); 1089193401Sed } 1090193401Sed } 1091198092Srdivacky 1092198092Srdivacky // If we are composing extended qualifiers together, merge together 1093198092Srdivacky // into one ExtQuals node. 1094198092Srdivacky QualifierCollector Quals; 1095198092Srdivacky const Type *TypeNode = Quals.strip(T); 1096198092Srdivacky 1097198092Srdivacky // If this type already has an ObjCGC specified, it cannot get 1098198092Srdivacky // another one. 1099198092Srdivacky assert(!Quals.hasObjCGCAttr() && 1100198092Srdivacky "Type cannot have multiple ObjCGCs!"); 1101198092Srdivacky Quals.addObjCGCAttr(GCAttr); 1102198092Srdivacky 1103198092Srdivacky return getExtQualType(TypeNode, Quals); 1104198092Srdivacky} 1105198092Srdivacky 1106198092SrdivackyQualType ASTContext::getNoReturnType(QualType T) { 1107198092Srdivacky QualType ResultType; 1108198092Srdivacky if (T->isPointerType()) { 1109198092Srdivacky QualType Pointee = T->getAs<PointerType>()->getPointeeType(); 1110198092Srdivacky ResultType = getNoReturnType(Pointee); 1111198092Srdivacky ResultType = getPointerType(ResultType); 1112198092Srdivacky } else if (T->isBlockPointerType()) { 1113198092Srdivacky QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType(); 1114198092Srdivacky ResultType = getNoReturnType(Pointee); 1115198092Srdivacky ResultType = getBlockPointerType(ResultType); 1116198092Srdivacky } else { 1117198092Srdivacky assert (T->isFunctionType() 1118198092Srdivacky && "can't noreturn qualify non-pointer to function or block type"); 1119198092Srdivacky 1120198092Srdivacky if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) { 1121198092Srdivacky ResultType = getFunctionNoProtoType(FNPT->getResultType(), true); 1122198092Srdivacky } else { 1123198092Srdivacky const FunctionProtoType *F = T->getAs<FunctionProtoType>(); 1124198092Srdivacky ResultType 1125198092Srdivacky = getFunctionType(F->getResultType(), F->arg_type_begin(), 1126198092Srdivacky F->getNumArgs(), F->isVariadic(), F->getTypeQuals(), 1127198092Srdivacky F->hasExceptionSpec(), F->hasAnyExceptionSpec(), 1128198092Srdivacky F->getNumExceptions(), F->exception_begin(), true); 1129198092Srdivacky } 1130193326Sed } 1131198092Srdivacky 1132198092Srdivacky return getQualifiedType(ResultType, T.getQualifiers()); 1133193326Sed} 1134193326Sed 1135193326Sed/// getComplexType - Return the uniqued reference to the type for a complex 1136193326Sed/// number with the specified element type. 1137193326SedQualType ASTContext::getComplexType(QualType T) { 1138193326Sed // Unique pointers, to guarantee there is only one pointer of a particular 1139193326Sed // structure. 1140193326Sed llvm::FoldingSetNodeID ID; 1141193326Sed ComplexType::Profile(ID, T); 1142198092Srdivacky 1143193326Sed void *InsertPos = 0; 1144193326Sed if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) 1145193326Sed return QualType(CT, 0); 1146198092Srdivacky 1147193326Sed // If the pointee type isn't canonical, this won't be a canonical type either, 1148193326Sed // so fill in the canonical type field. 1149193326Sed QualType Canonical; 1150198398Srdivacky if (!T.isCanonical()) { 1151193326Sed Canonical = getComplexType(getCanonicalType(T)); 1152198092Srdivacky 1153193326Sed // Get the new insert position for the node we care about. 1154193326Sed ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); 1155193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1156193326Sed } 1157198092Srdivacky ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); 1158193326Sed Types.push_back(New); 1159193326Sed ComplexTypes.InsertNode(New, InsertPos); 1160193326Sed return QualType(New, 0); 1161193326Sed} 1162193326Sed 1163193326SedQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { 1164193326Sed llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? 1165193326Sed SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; 1166193326Sed FixedWidthIntType *&Entry = Map[Width]; 1167193326Sed if (!Entry) 1168193326Sed Entry = new FixedWidthIntType(Width, Signed); 1169193326Sed return QualType(Entry, 0); 1170193326Sed} 1171193326Sed 1172193326Sed/// getPointerType - Return the uniqued reference to the type for a pointer to 1173193326Sed/// the specified type. 1174193326SedQualType ASTContext::getPointerType(QualType T) { 1175193326Sed // Unique pointers, to guarantee there is only one pointer of a particular 1176193326Sed // structure. 1177193326Sed llvm::FoldingSetNodeID ID; 1178193326Sed PointerType::Profile(ID, T); 1179198092Srdivacky 1180193326Sed void *InsertPos = 0; 1181193326Sed if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1182193326Sed return QualType(PT, 0); 1183198092Srdivacky 1184193326Sed // If the pointee type isn't canonical, this won't be a canonical type either, 1185193326Sed // so fill in the canonical type field. 1186193326Sed QualType Canonical; 1187198398Srdivacky if (!T.isCanonical()) { 1188193326Sed Canonical = getPointerType(getCanonicalType(T)); 1189198092Srdivacky 1190193326Sed // Get the new insert position for the node we care about. 1191193326Sed PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1192193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1193193326Sed } 1194198092Srdivacky PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); 1195193326Sed Types.push_back(New); 1196193326Sed PointerTypes.InsertNode(New, InsertPos); 1197193326Sed return QualType(New, 0); 1198193326Sed} 1199193326Sed 1200198092Srdivacky/// getBlockPointerType - Return the uniqued reference to the type for 1201193326Sed/// a pointer to the specified block. 1202193326SedQualType ASTContext::getBlockPointerType(QualType T) { 1203193326Sed assert(T->isFunctionType() && "block of function types only"); 1204193326Sed // Unique pointers, to guarantee there is only one block of a particular 1205193326Sed // structure. 1206193326Sed llvm::FoldingSetNodeID ID; 1207193326Sed BlockPointerType::Profile(ID, T); 1208198092Srdivacky 1209193326Sed void *InsertPos = 0; 1210193326Sed if (BlockPointerType *PT = 1211193326Sed BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1212193326Sed return QualType(PT, 0); 1213198092Srdivacky 1214198092Srdivacky // If the block pointee type isn't canonical, this won't be a canonical 1215193326Sed // type either so fill in the canonical type field. 1216193326Sed QualType Canonical; 1217198398Srdivacky if (!T.isCanonical()) { 1218193326Sed Canonical = getBlockPointerType(getCanonicalType(T)); 1219198092Srdivacky 1220193326Sed // Get the new insert position for the node we care about. 1221193326Sed BlockPointerType *NewIP = 1222193326Sed BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1223193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1224193326Sed } 1225198092Srdivacky BlockPointerType *New 1226198092Srdivacky = new (*this, TypeAlignment) BlockPointerType(T, Canonical); 1227193326Sed Types.push_back(New); 1228193326Sed BlockPointerTypes.InsertNode(New, InsertPos); 1229193326Sed return QualType(New, 0); 1230193326Sed} 1231193326Sed 1232193326Sed/// getLValueReferenceType - Return the uniqued reference to the type for an 1233193326Sed/// lvalue reference to the specified type. 1234198398SrdivackyQualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { 1235193326Sed // Unique pointers, to guarantee there is only one pointer of a particular 1236193326Sed // structure. 1237193326Sed llvm::FoldingSetNodeID ID; 1238198398Srdivacky ReferenceType::Profile(ID, T, SpelledAsLValue); 1239193326Sed 1240193326Sed void *InsertPos = 0; 1241193326Sed if (LValueReferenceType *RT = 1242193326Sed LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 1243193326Sed return QualType(RT, 0); 1244193326Sed 1245198398Srdivacky const ReferenceType *InnerRef = T->getAs<ReferenceType>(); 1246198398Srdivacky 1247193326Sed // If the referencee type isn't canonical, this won't be a canonical type 1248193326Sed // either, so fill in the canonical type field. 1249193326Sed QualType Canonical; 1250198398Srdivacky if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { 1251198398Srdivacky QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); 1252198398Srdivacky Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); 1253193326Sed 1254193326Sed // Get the new insert position for the node we care about. 1255193326Sed LValueReferenceType *NewIP = 1256193326Sed LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 1257193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1258193326Sed } 1259193326Sed 1260198092Srdivacky LValueReferenceType *New 1261198398Srdivacky = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, 1262198398Srdivacky SpelledAsLValue); 1263193326Sed Types.push_back(New); 1264193326Sed LValueReferenceTypes.InsertNode(New, InsertPos); 1265198398Srdivacky 1266193326Sed return QualType(New, 0); 1267193326Sed} 1268193326Sed 1269193326Sed/// getRValueReferenceType - Return the uniqued reference to the type for an 1270193326Sed/// rvalue reference to the specified type. 1271193326SedQualType ASTContext::getRValueReferenceType(QualType T) { 1272193326Sed // Unique pointers, to guarantee there is only one pointer of a particular 1273193326Sed // structure. 1274193326Sed llvm::FoldingSetNodeID ID; 1275198398Srdivacky ReferenceType::Profile(ID, T, false); 1276193326Sed 1277193326Sed void *InsertPos = 0; 1278193326Sed if (RValueReferenceType *RT = 1279193326Sed RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 1280193326Sed return QualType(RT, 0); 1281193326Sed 1282198398Srdivacky const ReferenceType *InnerRef = T->getAs<ReferenceType>(); 1283198398Srdivacky 1284193326Sed // If the referencee type isn't canonical, this won't be a canonical type 1285193326Sed // either, so fill in the canonical type field. 1286193326Sed QualType Canonical; 1287198398Srdivacky if (InnerRef || !T.isCanonical()) { 1288198398Srdivacky QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); 1289198398Srdivacky Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); 1290193326Sed 1291193326Sed // Get the new insert position for the node we care about. 1292193326Sed RValueReferenceType *NewIP = 1293193326Sed RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 1294193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1295193326Sed } 1296193326Sed 1297198092Srdivacky RValueReferenceType *New 1298198092Srdivacky = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); 1299193326Sed Types.push_back(New); 1300193326Sed RValueReferenceTypes.InsertNode(New, InsertPos); 1301193326Sed return QualType(New, 0); 1302193326Sed} 1303193326Sed 1304193326Sed/// getMemberPointerType - Return the uniqued reference to the type for a 1305193326Sed/// member pointer to the specified type, in the specified class. 1306198092SrdivackyQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { 1307193326Sed // Unique pointers, to guarantee there is only one pointer of a particular 1308193326Sed // structure. 1309193326Sed llvm::FoldingSetNodeID ID; 1310193326Sed MemberPointerType::Profile(ID, T, Cls); 1311193326Sed 1312193326Sed void *InsertPos = 0; 1313193326Sed if (MemberPointerType *PT = 1314193326Sed MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1315193326Sed return QualType(PT, 0); 1316193326Sed 1317193326Sed // If the pointee or class type isn't canonical, this won't be a canonical 1318193326Sed // type either, so fill in the canonical type field. 1319193326Sed QualType Canonical; 1320198398Srdivacky if (!T.isCanonical()) { 1321193326Sed Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); 1322193326Sed 1323193326Sed // Get the new insert position for the node we care about. 1324193326Sed MemberPointerType *NewIP = 1325193326Sed MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1326193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1327193326Sed } 1328198092Srdivacky MemberPointerType *New 1329198092Srdivacky = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); 1330193326Sed Types.push_back(New); 1331193326Sed MemberPointerTypes.InsertNode(New, InsertPos); 1332193326Sed return QualType(New, 0); 1333193326Sed} 1334193326Sed 1335198092Srdivacky/// getConstantArrayType - Return the unique reference to the type for an 1336193326Sed/// array of the specified element type. 1337198092SrdivackyQualType ASTContext::getConstantArrayType(QualType EltTy, 1338193326Sed const llvm::APInt &ArySizeIn, 1339193326Sed ArrayType::ArraySizeModifier ASM, 1340193326Sed unsigned EltTypeQuals) { 1341193326Sed assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) && 1342193326Sed "Constant array of VLAs is illegal!"); 1343193326Sed 1344193326Sed // Convert the array size into a canonical width matching the pointer size for 1345193326Sed // the target. 1346193326Sed llvm::APInt ArySize(ArySizeIn); 1347193326Sed ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); 1348198092Srdivacky 1349193326Sed llvm::FoldingSetNodeID ID; 1350193326Sed ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); 1351198092Srdivacky 1352193326Sed void *InsertPos = 0; 1353198092Srdivacky if (ConstantArrayType *ATP = 1354193326Sed ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 1355193326Sed return QualType(ATP, 0); 1356198092Srdivacky 1357193326Sed // If the element type isn't canonical, this won't be a canonical type either, 1358193326Sed // so fill in the canonical type field. 1359193326Sed QualType Canonical; 1360198398Srdivacky if (!EltTy.isCanonical()) { 1361198092Srdivacky Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, 1362193326Sed ASM, EltTypeQuals); 1363193326Sed // Get the new insert position for the node we care about. 1364198092Srdivacky ConstantArrayType *NewIP = 1365193326Sed ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1366193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1367193326Sed } 1368198092Srdivacky 1369198092Srdivacky ConstantArrayType *New = new(*this,TypeAlignment) 1370198092Srdivacky ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); 1371193326Sed ConstantArrayTypes.InsertNode(New, InsertPos); 1372193326Sed Types.push_back(New); 1373193326Sed return QualType(New, 0); 1374193326Sed} 1375193326Sed 1376193326Sed/// getVariableArrayType - Returns a non-unique reference to the type for a 1377193326Sed/// variable array of the specified element type. 1378198092SrdivackyQualType ASTContext::getVariableArrayType(QualType EltTy, 1379198092Srdivacky Expr *NumElts, 1380193326Sed ArrayType::ArraySizeModifier ASM, 1381198092Srdivacky unsigned EltTypeQuals, 1382198092Srdivacky SourceRange Brackets) { 1383193326Sed // Since we don't unique expressions, it isn't possible to unique VLA's 1384193326Sed // that have an expression provided for their size. 1385193326Sed 1386198092Srdivacky VariableArrayType *New = new(*this, TypeAlignment) 1387198092Srdivacky VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); 1388193326Sed 1389193326Sed VariableArrayTypes.push_back(New); 1390193326Sed Types.push_back(New); 1391193326Sed return QualType(New, 0); 1392193326Sed} 1393193326Sed 1394193326Sed/// getDependentSizedArrayType - Returns a non-unique reference to 1395193326Sed/// the type for a dependently-sized array of the specified element 1396198092Srdivacky/// type. 1397198092SrdivackyQualType ASTContext::getDependentSizedArrayType(QualType EltTy, 1398198092Srdivacky Expr *NumElts, 1399193326Sed ArrayType::ArraySizeModifier ASM, 1400198092Srdivacky unsigned EltTypeQuals, 1401198092Srdivacky SourceRange Brackets) { 1402198092Srdivacky assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && 1403193326Sed "Size must be type- or value-dependent!"); 1404193326Sed 1405198092Srdivacky llvm::FoldingSetNodeID ID; 1406198092Srdivacky DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, 1407198092Srdivacky EltTypeQuals, NumElts); 1408193326Sed 1409198092Srdivacky void *InsertPos = 0; 1410198092Srdivacky DependentSizedArrayType *Canon 1411198092Srdivacky = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1412198092Srdivacky DependentSizedArrayType *New; 1413198092Srdivacky if (Canon) { 1414198092Srdivacky // We already have a canonical version of this array type; use it as 1415198092Srdivacky // the canonical type for a newly-built type. 1416198092Srdivacky New = new (*this, TypeAlignment) 1417198092Srdivacky DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), 1418198092Srdivacky NumElts, ASM, EltTypeQuals, Brackets); 1419198092Srdivacky } else { 1420198092Srdivacky QualType CanonEltTy = getCanonicalType(EltTy); 1421198092Srdivacky if (CanonEltTy == EltTy) { 1422198092Srdivacky New = new (*this, TypeAlignment) 1423198092Srdivacky DependentSizedArrayType(*this, EltTy, QualType(), 1424198092Srdivacky NumElts, ASM, EltTypeQuals, Brackets); 1425198092Srdivacky DependentSizedArrayTypes.InsertNode(New, InsertPos); 1426198092Srdivacky } else { 1427198092Srdivacky QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, 1428198092Srdivacky ASM, EltTypeQuals, 1429198092Srdivacky SourceRange()); 1430198092Srdivacky New = new (*this, TypeAlignment) 1431198092Srdivacky DependentSizedArrayType(*this, EltTy, Canon, 1432198092Srdivacky NumElts, ASM, EltTypeQuals, Brackets); 1433198092Srdivacky } 1434198092Srdivacky } 1435193326Sed 1436193326Sed Types.push_back(New); 1437193326Sed return QualType(New, 0); 1438193326Sed} 1439193326Sed 1440193326SedQualType ASTContext::getIncompleteArrayType(QualType EltTy, 1441193326Sed ArrayType::ArraySizeModifier ASM, 1442193326Sed unsigned EltTypeQuals) { 1443193326Sed llvm::FoldingSetNodeID ID; 1444193326Sed IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); 1445193326Sed 1446193326Sed void *InsertPos = 0; 1447198092Srdivacky if (IncompleteArrayType *ATP = 1448193326Sed IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 1449193326Sed return QualType(ATP, 0); 1450193326Sed 1451193326Sed // If the element type isn't canonical, this won't be a canonical type 1452193326Sed // either, so fill in the canonical type field. 1453193326Sed QualType Canonical; 1454193326Sed 1455198398Srdivacky if (!EltTy.isCanonical()) { 1456193326Sed Canonical = getIncompleteArrayType(getCanonicalType(EltTy), 1457193326Sed ASM, EltTypeQuals); 1458193326Sed 1459193326Sed // Get the new insert position for the node we care about. 1460193326Sed IncompleteArrayType *NewIP = 1461193326Sed IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1462193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1463193326Sed } 1464193326Sed 1465198092Srdivacky IncompleteArrayType *New = new (*this, TypeAlignment) 1466198092Srdivacky IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); 1467193326Sed 1468193326Sed IncompleteArrayTypes.InsertNode(New, InsertPos); 1469193326Sed Types.push_back(New); 1470193326Sed return QualType(New, 0); 1471193326Sed} 1472193326Sed 1473193326Sed/// getVectorType - Return the unique reference to a vector type of 1474193326Sed/// the specified element type and size. VectorType must be a built-in type. 1475193326SedQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { 1476193326Sed BuiltinType *baseType; 1477198092Srdivacky 1478193326Sed baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 1479193326Sed assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); 1480198092Srdivacky 1481193326Sed // Check if we've already instantiated a vector of this type. 1482193326Sed llvm::FoldingSetNodeID ID; 1483198092Srdivacky VectorType::Profile(ID, vecType, NumElts, Type::Vector); 1484193326Sed void *InsertPos = 0; 1485193326Sed if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 1486193326Sed return QualType(VTP, 0); 1487193326Sed 1488193326Sed // If the element type isn't canonical, this won't be a canonical type either, 1489193326Sed // so fill in the canonical type field. 1490193326Sed QualType Canonical; 1491198398Srdivacky if (!vecType.isCanonical()) { 1492193326Sed Canonical = getVectorType(getCanonicalType(vecType), NumElts); 1493198092Srdivacky 1494193326Sed // Get the new insert position for the node we care about. 1495193326Sed VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1496193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1497193326Sed } 1498198092Srdivacky VectorType *New = new (*this, TypeAlignment) 1499198092Srdivacky VectorType(vecType, NumElts, Canonical); 1500193326Sed VectorTypes.InsertNode(New, InsertPos); 1501193326Sed Types.push_back(New); 1502193326Sed return QualType(New, 0); 1503193326Sed} 1504193326Sed 1505193326Sed/// getExtVectorType - Return the unique reference to an extended vector type of 1506193326Sed/// the specified element type and size. VectorType must be a built-in type. 1507193326SedQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { 1508193326Sed BuiltinType *baseType; 1509198092Srdivacky 1510193326Sed baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 1511193326Sed assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); 1512198092Srdivacky 1513193326Sed // Check if we've already instantiated a vector of this type. 1514193326Sed llvm::FoldingSetNodeID ID; 1515198092Srdivacky VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); 1516193326Sed void *InsertPos = 0; 1517193326Sed if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 1518193326Sed return QualType(VTP, 0); 1519193326Sed 1520193326Sed // If the element type isn't canonical, this won't be a canonical type either, 1521193326Sed // so fill in the canonical type field. 1522193326Sed QualType Canonical; 1523198398Srdivacky if (!vecType.isCanonical()) { 1524193326Sed Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); 1525198092Srdivacky 1526193326Sed // Get the new insert position for the node we care about. 1527193326Sed VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1528193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1529193326Sed } 1530198092Srdivacky ExtVectorType *New = new (*this, TypeAlignment) 1531198092Srdivacky ExtVectorType(vecType, NumElts, Canonical); 1532193326Sed VectorTypes.InsertNode(New, InsertPos); 1533193326Sed Types.push_back(New); 1534193326Sed return QualType(New, 0); 1535193326Sed} 1536193326Sed 1537198092SrdivackyQualType ASTContext::getDependentSizedExtVectorType(QualType vecType, 1538194613Sed Expr *SizeExpr, 1539194613Sed SourceLocation AttrLoc) { 1540198092Srdivacky llvm::FoldingSetNodeID ID; 1541198092Srdivacky DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), 1542198092Srdivacky SizeExpr); 1543194613Sed 1544198092Srdivacky void *InsertPos = 0; 1545198092Srdivacky DependentSizedExtVectorType *Canon 1546198092Srdivacky = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1547198092Srdivacky DependentSizedExtVectorType *New; 1548198092Srdivacky if (Canon) { 1549198092Srdivacky // We already have a canonical version of this array type; use it as 1550198092Srdivacky // the canonical type for a newly-built type. 1551198092Srdivacky New = new (*this, TypeAlignment) 1552198092Srdivacky DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), 1553198092Srdivacky SizeExpr, AttrLoc); 1554198092Srdivacky } else { 1555198092Srdivacky QualType CanonVecTy = getCanonicalType(vecType); 1556198092Srdivacky if (CanonVecTy == vecType) { 1557198092Srdivacky New = new (*this, TypeAlignment) 1558198092Srdivacky DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, 1559198092Srdivacky AttrLoc); 1560198092Srdivacky DependentSizedExtVectorTypes.InsertNode(New, InsertPos); 1561198092Srdivacky } else { 1562198092Srdivacky QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, 1563198092Srdivacky SourceLocation()); 1564198092Srdivacky New = new (*this, TypeAlignment) 1565198092Srdivacky DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); 1566198092Srdivacky } 1567198092Srdivacky } 1568198092Srdivacky 1569194613Sed Types.push_back(New); 1570194613Sed return QualType(New, 0); 1571194613Sed} 1572194613Sed 1573193326Sed/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 1574193326Sed/// 1575198092SrdivackyQualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { 1576193326Sed // Unique functions, to guarantee there is only one function of a particular 1577193326Sed // structure. 1578193326Sed llvm::FoldingSetNodeID ID; 1579198092Srdivacky FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); 1580198092Srdivacky 1581193326Sed void *InsertPos = 0; 1582198092Srdivacky if (FunctionNoProtoType *FT = 1583193326Sed FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 1584193326Sed return QualType(FT, 0); 1585198092Srdivacky 1586193326Sed QualType Canonical; 1587198398Srdivacky if (!ResultTy.isCanonical()) { 1588198092Srdivacky Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); 1589198092Srdivacky 1590193326Sed // Get the new insert position for the node we care about. 1591193326Sed FunctionNoProtoType *NewIP = 1592193326Sed FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1593193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1594193326Sed } 1595198092Srdivacky 1596198092Srdivacky FunctionNoProtoType *New = new (*this, TypeAlignment) 1597198092Srdivacky FunctionNoProtoType(ResultTy, Canonical, NoReturn); 1598193326Sed Types.push_back(New); 1599193326Sed FunctionNoProtoTypes.InsertNode(New, InsertPos); 1600193326Sed return QualType(New, 0); 1601193326Sed} 1602193326Sed 1603193326Sed/// getFunctionType - Return a normal function type with a typed argument 1604193326Sed/// list. isVariadic indicates whether the argument list includes '...'. 1605193326SedQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, 1606193326Sed unsigned NumArgs, bool isVariadic, 1607193326Sed unsigned TypeQuals, bool hasExceptionSpec, 1608193326Sed bool hasAnyExceptionSpec, unsigned NumExs, 1609198092Srdivacky const QualType *ExArray, bool NoReturn) { 1610193326Sed // Unique functions, to guarantee there is only one function of a particular 1611193326Sed // structure. 1612193326Sed llvm::FoldingSetNodeID ID; 1613193326Sed FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, 1614193326Sed TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, 1615198092Srdivacky NumExs, ExArray, NoReturn); 1616193326Sed 1617193326Sed void *InsertPos = 0; 1618198092Srdivacky if (FunctionProtoType *FTP = 1619193326Sed FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 1620193326Sed return QualType(FTP, 0); 1621193326Sed 1622193326Sed // Determine whether the type being created is already canonical or not. 1623198398Srdivacky bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); 1624193326Sed for (unsigned i = 0; i != NumArgs && isCanonical; ++i) 1625198398Srdivacky if (!ArgArray[i].isCanonicalAsParam()) 1626193326Sed isCanonical = false; 1627193326Sed 1628193326Sed // If this type isn't canonical, get the canonical version of it. 1629193326Sed // The exception spec is not part of the canonical type. 1630193326Sed QualType Canonical; 1631193326Sed if (!isCanonical) { 1632193326Sed llvm::SmallVector<QualType, 16> CanonicalArgs; 1633193326Sed CanonicalArgs.reserve(NumArgs); 1634193326Sed for (unsigned i = 0; i != NumArgs; ++i) 1635198398Srdivacky CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); 1636193326Sed 1637193326Sed Canonical = getFunctionType(getCanonicalType(ResultTy), 1638193326Sed CanonicalArgs.data(), NumArgs, 1639198092Srdivacky isVariadic, TypeQuals, false, 1640198092Srdivacky false, 0, 0, NoReturn); 1641193326Sed 1642193326Sed // Get the new insert position for the node we care about. 1643193326Sed FunctionProtoType *NewIP = 1644193326Sed FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1645193326Sed assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1646193326Sed } 1647193326Sed 1648193326Sed // FunctionProtoType objects are allocated with extra bytes after them 1649193326Sed // for two variable size arrays (for parameter and exception types) at the 1650193326Sed // end of them. 1651198092Srdivacky FunctionProtoType *FTP = 1652193326Sed (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + 1653193326Sed NumArgs*sizeof(QualType) + 1654198092Srdivacky NumExs*sizeof(QualType), TypeAlignment); 1655193326Sed new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, 1656193326Sed TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, 1657198092Srdivacky ExArray, NumExs, Canonical, NoReturn); 1658193326Sed Types.push_back(FTP); 1659193326Sed FunctionProtoTypes.InsertNode(FTP, InsertPos); 1660193326Sed return QualType(FTP, 0); 1661193326Sed} 1662193326Sed 1663193326Sed/// getTypeDeclType - Return the unique reference to the type for the 1664193326Sed/// specified type declaration. 1665193326SedQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { 1666193326Sed assert(Decl && "Passed null for Decl param"); 1667193326Sed if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 1668198092Srdivacky 1669193326Sed if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) 1670193326Sed return getTypedefType(Typedef); 1671193326Sed else if (isa<TemplateTypeParmDecl>(Decl)) { 1672193326Sed assert(false && "Template type parameter types are always available."); 1673198092Srdivacky } else if (ObjCInterfaceDecl *ObjCInterface 1674198092Srdivacky = dyn_cast<ObjCInterfaceDecl>(Decl)) 1675193326Sed return getObjCInterfaceType(ObjCInterface); 1676193326Sed 1677193326Sed if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { 1678193326Sed if (PrevDecl) 1679193326Sed Decl->TypeForDecl = PrevDecl->TypeForDecl; 1680193326Sed else 1681198092Srdivacky Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); 1682198092Srdivacky } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { 1683193326Sed if (PrevDecl) 1684193326Sed Decl->TypeForDecl = PrevDecl->TypeForDecl; 1685193326Sed else 1686198092Srdivacky Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); 1687198092Srdivacky } else 1688193326Sed assert(false && "TypeDecl without a type?"); 1689193326Sed 1690193326Sed if (!PrevDecl) Types.push_back(Decl->TypeForDecl); 1691193326Sed return QualType(Decl->TypeForDecl, 0); 1692193326Sed} 1693193326Sed 1694193326Sed/// getTypedefType - Return the unique reference to the type for the 1695193326Sed/// specified typename decl. 1696193326SedQualType ASTContext::getTypedefType(TypedefDecl *Decl) { 1697193326Sed if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 1698198092Srdivacky 1699193326Sed QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); 1700198092Srdivacky Decl->TypeForDecl = new(*this, TypeAlignment) 1701198092Srdivacky TypedefType(Type::Typedef, Decl, Canonical); 1702193326Sed Types.push_back(Decl->TypeForDecl); 1703193326Sed return QualType(Decl->TypeForDecl, 0); 1704193326Sed} 1705193326Sed 1706198398Srdivacky/// \brief Retrieve a substitution-result type. 1707198398SrdivackyQualType 1708198398SrdivackyASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, 1709198398Srdivacky QualType Replacement) { 1710198398Srdivacky assert(Replacement.isCanonical() 1711198398Srdivacky && "replacement types must always be canonical"); 1712198398Srdivacky 1713198398Srdivacky llvm::FoldingSetNodeID ID; 1714198398Srdivacky SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); 1715198398Srdivacky void *InsertPos = 0; 1716198398Srdivacky SubstTemplateTypeParmType *SubstParm 1717198398Srdivacky = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); 1718198398Srdivacky 1719198398Srdivacky if (!SubstParm) { 1720198398Srdivacky SubstParm = new (*this, TypeAlignment) 1721198398Srdivacky SubstTemplateTypeParmType(Parm, Replacement); 1722198398Srdivacky Types.push_back(SubstParm); 1723198398Srdivacky SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); 1724198398Srdivacky } 1725198398Srdivacky 1726198398Srdivacky return QualType(SubstParm, 0); 1727198398Srdivacky} 1728198398Srdivacky 1729193326Sed/// \brief Retrieve the template type parameter type for a template 1730198092Srdivacky/// parameter or parameter pack with the given depth, index, and (optionally) 1731194613Sed/// name. 1732198092SrdivackyQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, 1733194613Sed bool ParameterPack, 1734193326Sed IdentifierInfo *Name) { 1735193326Sed llvm::FoldingSetNodeID ID; 1736194613Sed TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); 1737193326Sed void *InsertPos = 0; 1738198092Srdivacky TemplateTypeParmType *TypeParm 1739193326Sed = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); 1740193326Sed 1741193326Sed if (TypeParm) 1742193326Sed return QualType(TypeParm, 0); 1743198092Srdivacky 1744194613Sed if (Name) { 1745194613Sed QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); 1746198092Srdivacky TypeParm = new (*this, TypeAlignment) 1747198092Srdivacky TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); 1748194613Sed } else 1749198092Srdivacky TypeParm = new (*this, TypeAlignment) 1750198092Srdivacky TemplateTypeParmType(Depth, Index, ParameterPack); 1751193326Sed 1752193326Sed Types.push_back(TypeParm); 1753193326Sed TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); 1754193326Sed 1755193326Sed return QualType(TypeParm, 0); 1756193326Sed} 1757193326Sed 1758198092SrdivackyQualType 1759193326SedASTContext::getTemplateSpecializationType(TemplateName Template, 1760193326Sed const TemplateArgument *Args, 1761193326Sed unsigned NumArgs, 1762193326Sed QualType Canon) { 1763193326Sed if (!Canon.isNull()) 1764193326Sed Canon = getCanonicalType(Canon); 1765198092Srdivacky else { 1766198092Srdivacky // Build the canonical template specialization type. 1767198092Srdivacky TemplateName CanonTemplate = getCanonicalTemplateName(Template); 1768198092Srdivacky llvm::SmallVector<TemplateArgument, 4> CanonArgs; 1769198092Srdivacky CanonArgs.reserve(NumArgs); 1770198092Srdivacky for (unsigned I = 0; I != NumArgs; ++I) 1771198092Srdivacky CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); 1772193326Sed 1773198092Srdivacky // Determine whether this canonical template specialization type already 1774198092Srdivacky // exists. 1775198092Srdivacky llvm::FoldingSetNodeID ID; 1776198092Srdivacky TemplateSpecializationType::Profile(ID, CanonTemplate, 1777198092Srdivacky CanonArgs.data(), NumArgs, *this); 1778193326Sed 1779198092Srdivacky void *InsertPos = 0; 1780198092Srdivacky TemplateSpecializationType *Spec 1781198092Srdivacky = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); 1782198092Srdivacky 1783198092Srdivacky if (!Spec) { 1784198092Srdivacky // Allocate a new canonical template specialization type. 1785198092Srdivacky void *Mem = Allocate((sizeof(TemplateSpecializationType) + 1786198092Srdivacky sizeof(TemplateArgument) * NumArgs), 1787198092Srdivacky TypeAlignment); 1788198092Srdivacky Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, 1789198092Srdivacky CanonArgs.data(), NumArgs, 1790198092Srdivacky Canon); 1791198092Srdivacky Types.push_back(Spec); 1792198092Srdivacky TemplateSpecializationTypes.InsertNode(Spec, InsertPos); 1793198092Srdivacky } 1794198092Srdivacky 1795198092Srdivacky if (Canon.isNull()) 1796198092Srdivacky Canon = QualType(Spec, 0); 1797198092Srdivacky assert(Canon->isDependentType() && 1798198092Srdivacky "Non-dependent template-id type must have a canonical type"); 1799198092Srdivacky } 1800198092Srdivacky 1801198092Srdivacky // Allocate the (non-canonical) template specialization type, but don't 1802198092Srdivacky // try to unique it: these types typically have location information that 1803198092Srdivacky // we don't unique and don't want to lose. 1804198092Srdivacky void *Mem = Allocate((sizeof(TemplateSpecializationType) + 1805198092Srdivacky sizeof(TemplateArgument) * NumArgs), 1806198092Srdivacky TypeAlignment); 1807193326Sed TemplateSpecializationType *Spec 1808198092Srdivacky = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, 1809198092Srdivacky Canon); 1810193326Sed 1811193326Sed Types.push_back(Spec); 1812198092Srdivacky return QualType(Spec, 0); 1813193326Sed} 1814193326Sed 1815198092SrdivackyQualType 1816193326SedASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, 1817193326Sed QualType NamedType) { 1818193326Sed llvm::FoldingSetNodeID ID; 1819193326Sed QualifiedNameType::Profile(ID, NNS, NamedType); 1820193326Sed 1821193326Sed void *InsertPos = 0; 1822198092Srdivacky QualifiedNameType *T 1823193326Sed = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); 1824193326Sed if (T) 1825193326Sed return QualType(T, 0); 1826193326Sed 1827198092Srdivacky T = new (*this) QualifiedNameType(NNS, NamedType, 1828193326Sed getCanonicalType(NamedType)); 1829193326Sed Types.push_back(T); 1830193326Sed QualifiedNameTypes.InsertNode(T, InsertPos); 1831193326Sed return QualType(T, 0); 1832193326Sed} 1833193326Sed 1834198092SrdivackyQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, 1835193326Sed const IdentifierInfo *Name, 1836193326Sed QualType Canon) { 1837193326Sed assert(NNS->isDependent() && "nested-name-specifier must be dependent"); 1838193326Sed 1839193326Sed if (Canon.isNull()) { 1840193326Sed NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 1841193326Sed if (CanonNNS != NNS) 1842193326Sed Canon = getTypenameType(CanonNNS, Name); 1843193326Sed } 1844193326Sed 1845193326Sed llvm::FoldingSetNodeID ID; 1846193326Sed TypenameType::Profile(ID, NNS, Name); 1847193326Sed 1848193326Sed void *InsertPos = 0; 1849198092Srdivacky TypenameType *T 1850193326Sed = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); 1851193326Sed if (T) 1852193326Sed return QualType(T, 0); 1853193326Sed 1854193326Sed T = new (*this) TypenameType(NNS, Name, Canon); 1855193326Sed Types.push_back(T); 1856193326Sed TypenameTypes.InsertNode(T, InsertPos); 1857198092Srdivacky return QualType(T, 0); 1858193326Sed} 1859193326Sed 1860198092SrdivackyQualType 1861198092SrdivackyASTContext::getTypenameType(NestedNameSpecifier *NNS, 1862193326Sed const TemplateSpecializationType *TemplateId, 1863193326Sed QualType Canon) { 1864193326Sed assert(NNS->isDependent() && "nested-name-specifier must be dependent"); 1865193326Sed 1866193326Sed if (Canon.isNull()) { 1867193326Sed NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 1868193326Sed QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); 1869193326Sed if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { 1870193326Sed const TemplateSpecializationType *CanonTemplateId 1871198092Srdivacky = CanonType->getAs<TemplateSpecializationType>(); 1872193326Sed assert(CanonTemplateId && 1873193326Sed "Canonical type must also be a template specialization type"); 1874193326Sed Canon = getTypenameType(CanonNNS, CanonTemplateId); 1875193326Sed } 1876193326Sed } 1877193326Sed 1878193326Sed llvm::FoldingSetNodeID ID; 1879193326Sed TypenameType::Profile(ID, NNS, TemplateId); 1880193326Sed 1881193326Sed void *InsertPos = 0; 1882198092Srdivacky TypenameType *T 1883193326Sed = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); 1884193326Sed if (T) 1885193326Sed return QualType(T, 0); 1886193326Sed 1887193326Sed T = new (*this) TypenameType(NNS, TemplateId, Canon); 1888193326Sed Types.push_back(T); 1889193326Sed TypenameTypes.InsertNode(T, InsertPos); 1890198092Srdivacky return QualType(T, 0); 1891193326Sed} 1892193326Sed 1893198092SrdivackyQualType 1894198092SrdivackyASTContext::getElaboratedType(QualType UnderlyingType, 1895198092Srdivacky ElaboratedType::TagKind Tag) { 1896198092Srdivacky llvm::FoldingSetNodeID ID; 1897198092Srdivacky ElaboratedType::Profile(ID, UnderlyingType, Tag); 1898198092Srdivacky 1899198092Srdivacky void *InsertPos = 0; 1900198092Srdivacky ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); 1901198092Srdivacky if (T) 1902198092Srdivacky return QualType(T, 0); 1903198092Srdivacky 1904198092Srdivacky QualType Canon = getCanonicalType(UnderlyingType); 1905198092Srdivacky 1906198092Srdivacky T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); 1907198092Srdivacky Types.push_back(T); 1908198092Srdivacky ElaboratedTypes.InsertNode(T, InsertPos); 1909198092Srdivacky return QualType(T, 0); 1910198092Srdivacky} 1911198092Srdivacky 1912193326Sed/// CmpProtocolNames - Comparison predicate for sorting protocols 1913193326Sed/// alphabetically. 1914193326Sedstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS, 1915193326Sed const ObjCProtocolDecl *RHS) { 1916193326Sed return LHS->getDeclName() < RHS->getDeclName(); 1917193326Sed} 1918193326Sed 1919198398Srdivackystatic bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, 1920198398Srdivacky unsigned NumProtocols) { 1921198398Srdivacky if (NumProtocols == 0) return true; 1922198398Srdivacky 1923198398Srdivacky for (unsigned i = 1; i != NumProtocols; ++i) 1924198398Srdivacky if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) 1925198398Srdivacky return false; 1926198398Srdivacky return true; 1927198398Srdivacky} 1928198398Srdivacky 1929198398Srdivackystatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, 1930193326Sed unsigned &NumProtocols) { 1931193326Sed ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; 1932198092Srdivacky 1933193326Sed // Sort protocols, keyed by name. 1934193326Sed std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); 1935193326Sed 1936193326Sed // Remove duplicates. 1937193326Sed ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); 1938193326Sed NumProtocols = ProtocolsEnd-Protocols; 1939193326Sed} 1940193326Sed 1941194613Sed/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for 1942194613Sed/// the given interface decl and the conforming protocol list. 1943198092SrdivackyQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, 1944198092Srdivacky ObjCProtocolDecl **Protocols, 1945194613Sed unsigned NumProtocols) { 1946194613Sed llvm::FoldingSetNodeID ID; 1947198092Srdivacky ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); 1948194613Sed 1949194613Sed void *InsertPos = 0; 1950194613Sed if (ObjCObjectPointerType *QT = 1951194613Sed ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1952194613Sed return QualType(QT, 0); 1953194613Sed 1954198398Srdivacky // Sort the protocol list alphabetically to canonicalize it. 1955198398Srdivacky QualType Canonical; 1956198398Srdivacky if (!InterfaceT.isCanonical() || 1957198398Srdivacky !areSortedAndUniqued(Protocols, NumProtocols)) { 1958198398Srdivacky if (!areSortedAndUniqued(Protocols, NumProtocols)) { 1959198398Srdivacky llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); 1960198398Srdivacky unsigned UniqueCount = NumProtocols; 1961198398Srdivacky 1962198398Srdivacky std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); 1963198398Srdivacky SortAndUniqueProtocols(&Sorted[0], UniqueCount); 1964198398Srdivacky 1965198398Srdivacky Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), 1966198398Srdivacky &Sorted[0], UniqueCount); 1967198398Srdivacky } else { 1968198398Srdivacky Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), 1969198398Srdivacky Protocols, NumProtocols); 1970198398Srdivacky } 1971198398Srdivacky 1972198398Srdivacky // Regenerate InsertPos. 1973198398Srdivacky ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1974198398Srdivacky } 1975198398Srdivacky 1976194613Sed // No Match; 1977198092Srdivacky ObjCObjectPointerType *QType = new (*this, TypeAlignment) 1978198398Srdivacky ObjCObjectPointerType(Canonical, InterfaceT, Protocols, NumProtocols); 1979198092Srdivacky 1980194613Sed Types.push_back(QType); 1981194613Sed ObjCObjectPointerTypes.InsertNode(QType, InsertPos); 1982194613Sed return QualType(QType, 0); 1983194613Sed} 1984194613Sed 1985198092Srdivacky/// getObjCInterfaceType - Return the unique reference to the type for the 1986198092Srdivacky/// specified ObjC interface decl. The list of protocols is optional. 1987198092SrdivackyQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 1988193326Sed ObjCProtocolDecl **Protocols, unsigned NumProtocols) { 1989193326Sed llvm::FoldingSetNodeID ID; 1990198092Srdivacky ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); 1991198092Srdivacky 1992193326Sed void *InsertPos = 0; 1993198092Srdivacky if (ObjCInterfaceType *QT = 1994198092Srdivacky ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) 1995193326Sed return QualType(QT, 0); 1996198092Srdivacky 1997198398Srdivacky // Sort the protocol list alphabetically to canonicalize it. 1998198398Srdivacky QualType Canonical; 1999198398Srdivacky if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { 2000198398Srdivacky llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); 2001198398Srdivacky std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); 2002198398Srdivacky 2003198398Srdivacky unsigned UniqueCount = NumProtocols; 2004198398Srdivacky SortAndUniqueProtocols(&Sorted[0], UniqueCount); 2005198398Srdivacky 2006198398Srdivacky Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); 2007198398Srdivacky 2008198398Srdivacky ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); 2009198398Srdivacky } 2010198398Srdivacky 2011198092Srdivacky ObjCInterfaceType *QType = new (*this, TypeAlignment) 2012198398Srdivacky ObjCInterfaceType(Canonical, const_cast<ObjCInterfaceDecl*>(Decl), 2013198092Srdivacky Protocols, NumProtocols); 2014198398Srdivacky 2015198092Srdivacky Types.push_back(QType); 2016198092Srdivacky ObjCInterfaceTypes.InsertNode(QType, InsertPos); 2017198092Srdivacky return QualType(QType, 0); 2018198092Srdivacky} 2019193326Sed 2020193326Sed/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique 2021193326Sed/// TypeOfExprType AST's (since expression's are never shared). For example, 2022193326Sed/// multiple declarations that refer to "typeof(x)" all contain different 2023198092Srdivacky/// DeclRefExpr's. This doesn't effect the type checker, since it operates 2024193326Sed/// on canonical type's (which are always unique). 2025193326SedQualType ASTContext::getTypeOfExprType(Expr *tofExpr) { 2026198092Srdivacky TypeOfExprType *toe; 2027198092Srdivacky if (tofExpr->isTypeDependent()) { 2028198092Srdivacky llvm::FoldingSetNodeID ID; 2029198092Srdivacky DependentTypeOfExprType::Profile(ID, *this, tofExpr); 2030198092Srdivacky 2031198092Srdivacky void *InsertPos = 0; 2032198092Srdivacky DependentTypeOfExprType *Canon 2033198092Srdivacky = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); 2034198092Srdivacky if (Canon) { 2035198092Srdivacky // We already have a "canonical" version of an identical, dependent 2036198092Srdivacky // typeof(expr) type. Use that as our canonical type. 2037198092Srdivacky toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, 2038198092Srdivacky QualType((TypeOfExprType*)Canon, 0)); 2039198092Srdivacky } 2040198092Srdivacky else { 2041198092Srdivacky // Build a new, canonical typeof(expr) type. 2042198092Srdivacky Canon 2043198092Srdivacky = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); 2044198092Srdivacky DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); 2045198092Srdivacky toe = Canon; 2046198092Srdivacky } 2047198092Srdivacky } else { 2048198092Srdivacky QualType Canonical = getCanonicalType(tofExpr->getType()); 2049198092Srdivacky toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); 2050198092Srdivacky } 2051193326Sed Types.push_back(toe); 2052193326Sed return QualType(toe, 0); 2053193326Sed} 2054193326Sed 2055193326Sed/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique 2056193326Sed/// TypeOfType AST's. The only motivation to unique these nodes would be 2057193326Sed/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be 2058198092Srdivacky/// an issue. This doesn't effect the type checker, since it operates 2059193326Sed/// on canonical type's (which are always unique). 2060193326SedQualType ASTContext::getTypeOfType(QualType tofType) { 2061193326Sed QualType Canonical = getCanonicalType(tofType); 2062198092Srdivacky TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); 2063193326Sed Types.push_back(tot); 2064193326Sed return QualType(tot, 0); 2065193326Sed} 2066193326Sed 2067195099Sed/// getDecltypeForExpr - Given an expr, will return the decltype for that 2068195099Sed/// expression, according to the rules in C++0x [dcl.type.simple]p4 2069195099Sedstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { 2070195099Sed if (e->isTypeDependent()) 2071195099Sed return Context.DependentTy; 2072198092Srdivacky 2073195099Sed // If e is an id expression or a class member access, decltype(e) is defined 2074195099Sed // as the type of the entity named by e. 2075195099Sed if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { 2076195099Sed if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) 2077195099Sed return VD->getType(); 2078195099Sed } 2079195099Sed if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { 2080195099Sed if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) 2081195099Sed return FD->getType(); 2082195099Sed } 2083195099Sed // If e is a function call or an invocation of an overloaded operator, 2084195099Sed // (parentheses around e are ignored), decltype(e) is defined as the 2085195099Sed // return type of that function. 2086195099Sed if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) 2087195099Sed return CE->getCallReturnType(); 2088198092Srdivacky 2089195099Sed QualType T = e->getType(); 2090198092Srdivacky 2091198092Srdivacky // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is 2092195099Sed // defined as T&, otherwise decltype(e) is defined as T. 2093195099Sed if (e->isLvalue(Context) == Expr::LV_Valid) 2094195099Sed T = Context.getLValueReferenceType(T); 2095198092Srdivacky 2096195099Sed return T; 2097195099Sed} 2098195099Sed 2099195099Sed/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique 2100195099Sed/// DecltypeType AST's. The only motivation to unique these nodes would be 2101195099Sed/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be 2102198092Srdivacky/// an issue. This doesn't effect the type checker, since it operates 2103195099Sed/// on canonical type's (which are always unique). 2104195099SedQualType ASTContext::getDecltypeType(Expr *e) { 2105198092Srdivacky DecltypeType *dt; 2106198092Srdivacky if (e->isTypeDependent()) { 2107198092Srdivacky llvm::FoldingSetNodeID ID; 2108198092Srdivacky DependentDecltypeType::Profile(ID, *this, e); 2109198092Srdivacky 2110198092Srdivacky void *InsertPos = 0; 2111198092Srdivacky DependentDecltypeType *Canon 2112198092Srdivacky = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); 2113198092Srdivacky if (Canon) { 2114198092Srdivacky // We already have a "canonical" version of an equivalent, dependent 2115198092Srdivacky // decltype type. Use that as our canonical type. 2116198092Srdivacky dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, 2117198092Srdivacky QualType((DecltypeType*)Canon, 0)); 2118198092Srdivacky } 2119198092Srdivacky else { 2120198092Srdivacky // Build a new, canonical typeof(expr) type. 2121198092Srdivacky Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); 2122198092Srdivacky DependentDecltypeTypes.InsertNode(Canon, InsertPos); 2123198092Srdivacky dt = Canon; 2124198092Srdivacky } 2125198092Srdivacky } else { 2126198092Srdivacky QualType T = getDecltypeForExpr(e, *this); 2127198092Srdivacky dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); 2128198092Srdivacky } 2129195099Sed Types.push_back(dt); 2130195099Sed return QualType(dt, 0); 2131195099Sed} 2132195099Sed 2133193326Sed/// getTagDeclType - Return the unique reference to the type for the 2134193326Sed/// specified TagDecl (struct/union/class/enum) decl. 2135198092SrdivackyQualType ASTContext::getTagDeclType(const TagDecl *Decl) { 2136193326Sed assert (Decl); 2137198092Srdivacky // FIXME: What is the design on getTagDeclType when it requires casting 2138198092Srdivacky // away const? mutable? 2139198092Srdivacky return getTypeDeclType(const_cast<TagDecl*>(Decl)); 2140193326Sed} 2141193326Sed 2142198092Srdivacky/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result 2143198092Srdivacky/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and 2144198092Srdivacky/// needs to agree with the definition in <stddef.h>. 2145193326SedQualType ASTContext::getSizeType() const { 2146193326Sed return getFromTargetType(Target.getSizeType()); 2147193326Sed} 2148193326Sed 2149193326Sed/// getSignedWCharType - Return the type of "signed wchar_t". 2150193326Sed/// Used when in C++, as a GCC extension. 2151193326SedQualType ASTContext::getSignedWCharType() const { 2152193326Sed // FIXME: derive from "Target" ? 2153193326Sed return WCharTy; 2154193326Sed} 2155193326Sed 2156193326Sed/// getUnsignedWCharType - Return the type of "unsigned wchar_t". 2157193326Sed/// Used when in C++, as a GCC extension. 2158193326SedQualType ASTContext::getUnsignedWCharType() const { 2159193326Sed // FIXME: derive from "Target" ? 2160193326Sed return UnsignedIntTy; 2161193326Sed} 2162193326Sed 2163193326Sed/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 2164193326Sed/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 2165193326SedQualType ASTContext::getPointerDiffType() const { 2166193326Sed return getFromTargetType(Target.getPtrDiffType(0)); 2167193326Sed} 2168193326Sed 2169193326Sed//===----------------------------------------------------------------------===// 2170193326Sed// Type Operators 2171193326Sed//===----------------------------------------------------------------------===// 2172193326Sed 2173198398SrdivackyCanQualType ASTContext::getCanonicalParamType(QualType T) { 2174198398Srdivacky // Push qualifiers into arrays, and then discard any remaining 2175198398Srdivacky // qualifiers. 2176198398Srdivacky T = getCanonicalType(T); 2177198398Srdivacky const Type *Ty = T.getTypePtr(); 2178198398Srdivacky 2179198398Srdivacky QualType Result; 2180198398Srdivacky if (isa<ArrayType>(Ty)) { 2181198398Srdivacky Result = getArrayDecayedType(QualType(Ty,0)); 2182198398Srdivacky } else if (isa<FunctionType>(Ty)) { 2183198398Srdivacky Result = getPointerType(QualType(Ty, 0)); 2184198398Srdivacky } else { 2185198398Srdivacky Result = QualType(Ty, 0); 2186198398Srdivacky } 2187198398Srdivacky 2188198398Srdivacky return CanQualType::CreateUnsafe(Result); 2189198398Srdivacky} 2190198398Srdivacky 2191193326Sed/// getCanonicalType - Return the canonical (structural) type corresponding to 2192193326Sed/// the specified potentially non-canonical type. The non-canonical version 2193193326Sed/// of a type may have many "decorated" versions of types. Decorators can 2194193326Sed/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 2195193326Sed/// to be free of any of these, allowing two canonical types to be compared 2196193326Sed/// for exact equality with a simple pointer comparison. 2197198092SrdivackyCanQualType ASTContext::getCanonicalType(QualType T) { 2198198092Srdivacky QualifierCollector Quals; 2199198092Srdivacky const Type *Ptr = Quals.strip(T); 2200198092Srdivacky QualType CanType = Ptr->getCanonicalTypeInternal(); 2201193326Sed 2202198092Srdivacky // The canonical internal type will be the canonical type *except* 2203198092Srdivacky // that we push type qualifiers down through array types. 2204198092Srdivacky 2205198092Srdivacky // If there are no new qualifiers to push down, stop here. 2206198092Srdivacky if (!Quals.hasQualifiers()) 2207198092Srdivacky return CanQualType::CreateUnsafe(CanType); 2208198092Srdivacky 2209198092Srdivacky // If the type qualifiers are on an array type, get the canonical 2210198092Srdivacky // type of the array with the qualifiers applied to the element 2211198092Srdivacky // type. 2212193326Sed ArrayType *AT = dyn_cast<ArrayType>(CanType); 2213193326Sed if (!AT) 2214198092Srdivacky return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); 2215198092Srdivacky 2216193326Sed // Get the canonical version of the element with the extra qualifiers on it. 2217193326Sed // This can recursively sink qualifiers through multiple levels of arrays. 2218198092Srdivacky QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); 2219193326Sed NewEltTy = getCanonicalType(NewEltTy); 2220198092Srdivacky 2221193326Sed if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 2222198092Srdivacky return CanQualType::CreateUnsafe( 2223198092Srdivacky getConstantArrayType(NewEltTy, CAT->getSize(), 2224198092Srdivacky CAT->getSizeModifier(), 2225198092Srdivacky CAT->getIndexTypeCVRQualifiers())); 2226193326Sed if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) 2227198092Srdivacky return CanQualType::CreateUnsafe( 2228198092Srdivacky getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), 2229198092Srdivacky IAT->getIndexTypeCVRQualifiers())); 2230198092Srdivacky 2231193326Sed if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) 2232198092Srdivacky return CanQualType::CreateUnsafe( 2233198092Srdivacky getDependentSizedArrayType(NewEltTy, 2234198092Srdivacky DSAT->getSizeExpr() ? 2235198092Srdivacky DSAT->getSizeExpr()->Retain() : 0, 2236198092Srdivacky DSAT->getSizeModifier(), 2237198092Srdivacky DSAT->getIndexTypeCVRQualifiers(), 2238198092Srdivacky DSAT->getBracketsRange())); 2239193326Sed 2240193326Sed VariableArrayType *VAT = cast<VariableArrayType>(AT); 2241198092Srdivacky return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, 2242198092Srdivacky VAT->getSizeExpr() ? 2243198092Srdivacky VAT->getSizeExpr()->Retain() : 0, 2244198092Srdivacky VAT->getSizeModifier(), 2245198092Srdivacky VAT->getIndexTypeCVRQualifiers(), 2246198092Srdivacky VAT->getBracketsRange())); 2247193326Sed} 2248193326Sed 2249198092SrdivackyTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { 2250198092Srdivacky // If this template name refers to a template, the canonical 2251198092Srdivacky // template name merely stores the template itself. 2252198092Srdivacky if (TemplateDecl *Template = Name.getAsTemplateDecl()) 2253198092Srdivacky return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); 2254193326Sed 2255198092Srdivacky // If this template name refers to a set of overloaded function templates, 2256198092Srdivacky /// the canonical template name merely stores the set of function templates. 2257198092Srdivacky if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) { 2258198092Srdivacky OverloadedFunctionDecl *CanonOvl = 0; 2259198092Srdivacky for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), 2260198092Srdivacky FEnd = Ovl->function_end(); 2261198092Srdivacky F != FEnd; ++F) { 2262198092Srdivacky Decl *Canon = F->get()->getCanonicalDecl(); 2263198092Srdivacky if (CanonOvl || Canon != F->get()) { 2264198092Srdivacky if (!CanonOvl) 2265198092Srdivacky CanonOvl = OverloadedFunctionDecl::Create(*this, 2266198092Srdivacky Ovl->getDeclContext(), 2267198092Srdivacky Ovl->getDeclName()); 2268193326Sed 2269198092Srdivacky CanonOvl->addOverload( 2270198092Srdivacky AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon))); 2271198092Srdivacky } 2272198092Srdivacky } 2273193326Sed 2274198092Srdivacky return TemplateName(CanonOvl? CanonOvl : Ovl); 2275193326Sed } 2276193326Sed 2277193326Sed DependentTemplateName *DTN = Name.getAsDependentTemplateName(); 2278193326Sed assert(DTN && "Non-dependent template names must refer to template decls."); 2279193326Sed return DTN->CanonicalTemplateName; 2280193326Sed} 2281193326Sed 2282198092SrdivackyTemplateArgument 2283198092SrdivackyASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { 2284198092Srdivacky switch (Arg.getKind()) { 2285198092Srdivacky case TemplateArgument::Null: 2286198092Srdivacky return Arg; 2287198092Srdivacky 2288198092Srdivacky case TemplateArgument::Expression: 2289198092Srdivacky // FIXME: Build canonical expression? 2290198092Srdivacky return Arg; 2291198092Srdivacky 2292198092Srdivacky case TemplateArgument::Declaration: 2293198092Srdivacky return TemplateArgument(SourceLocation(), 2294198092Srdivacky Arg.getAsDecl()->getCanonicalDecl()); 2295198092Srdivacky 2296198092Srdivacky case TemplateArgument::Integral: 2297198092Srdivacky return TemplateArgument(SourceLocation(), 2298198092Srdivacky *Arg.getAsIntegral(), 2299198092Srdivacky getCanonicalType(Arg.getIntegralType())); 2300198092Srdivacky 2301198092Srdivacky case TemplateArgument::Type: 2302198092Srdivacky return TemplateArgument(SourceLocation(), 2303198092Srdivacky getCanonicalType(Arg.getAsType())); 2304198092Srdivacky 2305198092Srdivacky case TemplateArgument::Pack: { 2306198092Srdivacky // FIXME: Allocate in ASTContext 2307198092Srdivacky TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; 2308198092Srdivacky unsigned Idx = 0; 2309198092Srdivacky for (TemplateArgument::pack_iterator A = Arg.pack_begin(), 2310198092Srdivacky AEnd = Arg.pack_end(); 2311198092Srdivacky A != AEnd; (void)++A, ++Idx) 2312198092Srdivacky CanonArgs[Idx] = getCanonicalTemplateArgument(*A); 2313198092Srdivacky 2314198092Srdivacky TemplateArgument Result; 2315198092Srdivacky Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); 2316198092Srdivacky return Result; 2317198092Srdivacky } 2318198092Srdivacky } 2319198092Srdivacky 2320198092Srdivacky // Silence GCC warning 2321198092Srdivacky assert(false && "Unhandled template argument kind"); 2322198092Srdivacky return TemplateArgument(); 2323198092Srdivacky} 2324198092Srdivacky 2325193326SedNestedNameSpecifier * 2326193326SedASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { 2327198092Srdivacky if (!NNS) 2328193326Sed return 0; 2329193326Sed 2330193326Sed switch (NNS->getKind()) { 2331193326Sed case NestedNameSpecifier::Identifier: 2332193326Sed // Canonicalize the prefix but keep the identifier the same. 2333198092Srdivacky return NestedNameSpecifier::Create(*this, 2334193326Sed getCanonicalNestedNameSpecifier(NNS->getPrefix()), 2335193326Sed NNS->getAsIdentifier()); 2336193326Sed 2337193326Sed case NestedNameSpecifier::Namespace: 2338193326Sed // A namespace is canonical; build a nested-name-specifier with 2339193326Sed // this namespace and no prefix. 2340193326Sed return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); 2341193326Sed 2342193326Sed case NestedNameSpecifier::TypeSpec: 2343193326Sed case NestedNameSpecifier::TypeSpecWithTemplate: { 2344193326Sed QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); 2345198092Srdivacky return NestedNameSpecifier::Create(*this, 0, 2346198092Srdivacky NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, 2347193326Sed T.getTypePtr()); 2348193326Sed } 2349193326Sed 2350193326Sed case NestedNameSpecifier::Global: 2351193326Sed // The global specifier is canonical and unique. 2352193326Sed return NNS; 2353193326Sed } 2354193326Sed 2355193326Sed // Required to silence a GCC warning 2356193326Sed return 0; 2357193326Sed} 2358193326Sed 2359193326Sed 2360193326Sedconst ArrayType *ASTContext::getAsArrayType(QualType T) { 2361193326Sed // Handle the non-qualified case efficiently. 2362198092Srdivacky if (!T.hasQualifiers()) { 2363193326Sed // Handle the common positive case fast. 2364193326Sed if (const ArrayType *AT = dyn_cast<ArrayType>(T)) 2365193326Sed return AT; 2366193326Sed } 2367198092Srdivacky 2368198092Srdivacky // Handle the common negative case fast. 2369193326Sed QualType CType = T->getCanonicalTypeInternal(); 2370198092Srdivacky if (!isa<ArrayType>(CType)) 2371193326Sed return 0; 2372198092Srdivacky 2373198092Srdivacky // Apply any qualifiers from the array type to the element type. This 2374193326Sed // implements C99 6.7.3p8: "If the specification of an array type includes 2375193326Sed // any type qualifiers, the element type is so qualified, not the array type." 2376198092Srdivacky 2377193326Sed // If we get here, we either have type qualifiers on the type, or we have 2378193326Sed // sugar such as a typedef in the way. If we have type qualifiers on the type 2379198092Srdivacky // we must propagate them down into the element type. 2380198092Srdivacky 2381198092Srdivacky QualifierCollector Qs; 2382198092Srdivacky const Type *Ty = Qs.strip(T.getDesugaredType()); 2383198092Srdivacky 2384193326Sed // If we have a simple case, just return now. 2385193326Sed const ArrayType *ATy = dyn_cast<ArrayType>(Ty); 2386198092Srdivacky if (ATy == 0 || Qs.empty()) 2387193326Sed return ATy; 2388198092Srdivacky 2389193326Sed // Otherwise, we have an array and we have qualifiers on it. Push the 2390193326Sed // qualifiers into the array element type and return a new array type. 2391193326Sed // Get the canonical version of the element with the extra qualifiers on it. 2392193326Sed // This can recursively sink qualifiers through multiple levels of arrays. 2393198092Srdivacky QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); 2394198092Srdivacky 2395193326Sed if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) 2396193326Sed return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), 2397193326Sed CAT->getSizeModifier(), 2398198092Srdivacky CAT->getIndexTypeCVRQualifiers())); 2399193326Sed if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) 2400193326Sed return cast<ArrayType>(getIncompleteArrayType(NewEltTy, 2401193326Sed IAT->getSizeModifier(), 2402198092Srdivacky IAT->getIndexTypeCVRQualifiers())); 2403193326Sed 2404198092Srdivacky if (const DependentSizedArrayType *DSAT 2405193326Sed = dyn_cast<DependentSizedArrayType>(ATy)) 2406193326Sed return cast<ArrayType>( 2407198092Srdivacky getDependentSizedArrayType(NewEltTy, 2408198092Srdivacky DSAT->getSizeExpr() ? 2409198092Srdivacky DSAT->getSizeExpr()->Retain() : 0, 2410193326Sed DSAT->getSizeModifier(), 2411198092Srdivacky DSAT->getIndexTypeCVRQualifiers(), 2412198092Srdivacky DSAT->getBracketsRange())); 2413198092Srdivacky 2414193326Sed const VariableArrayType *VAT = cast<VariableArrayType>(ATy); 2415198092Srdivacky return cast<ArrayType>(getVariableArrayType(NewEltTy, 2416198092Srdivacky VAT->getSizeExpr() ? 2417198092Srdivacky VAT->getSizeExpr()->Retain() : 0, 2418193326Sed VAT->getSizeModifier(), 2419198092Srdivacky VAT->getIndexTypeCVRQualifiers(), 2420198092Srdivacky VAT->getBracketsRange())); 2421193326Sed} 2422193326Sed 2423193326Sed 2424193326Sed/// getArrayDecayedType - Return the properly qualified result of decaying the 2425193326Sed/// specified array type to a pointer. This operation is non-trivial when 2426193326Sed/// handling typedefs etc. The canonical type of "T" must be an array type, 2427193326Sed/// this returns a pointer to a properly qualified element of the array. 2428193326Sed/// 2429193326Sed/// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 2430193326SedQualType ASTContext::getArrayDecayedType(QualType Ty) { 2431193326Sed // Get the element type with 'getAsArrayType' so that we don't lose any 2432193326Sed // typedefs in the element type of the array. This also handles propagation 2433193326Sed // of type qualifiers from the array type into the element type if present 2434193326Sed // (C99 6.7.3p8). 2435193326Sed const ArrayType *PrettyArrayType = getAsArrayType(Ty); 2436193326Sed assert(PrettyArrayType && "Not an array type!"); 2437198092Srdivacky 2438193326Sed QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); 2439193326Sed 2440193326Sed // int x[restrict 4] -> int *restrict 2441198092Srdivacky return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); 2442193326Sed} 2443193326Sed 2444198092SrdivackyQualType ASTContext::getBaseElementType(QualType QT) { 2445198092Srdivacky QualifierCollector Qs; 2446198092Srdivacky while (true) { 2447198092Srdivacky const Type *UT = Qs.strip(QT); 2448198092Srdivacky if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { 2449198092Srdivacky QT = AT->getElementType(); 2450198092Srdivacky } else { 2451198092Srdivacky return Qs.apply(QT); 2452198092Srdivacky } 2453198092Srdivacky } 2454198092Srdivacky} 2455198092Srdivacky 2456198092SrdivackyQualType ASTContext::getBaseElementType(const ArrayType *AT) { 2457198092Srdivacky QualType ElemTy = AT->getElementType(); 2458198092Srdivacky 2459198092Srdivacky if (const ArrayType *AT = getAsArrayType(ElemTy)) 2460198092Srdivacky return getBaseElementType(AT); 2461198092Srdivacky 2462193326Sed return ElemTy; 2463193326Sed} 2464193326Sed 2465198092Srdivacky/// getConstantArrayElementCount - Returns number of constant array elements. 2466198092Srdivackyuint64_t 2467198092SrdivackyASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { 2468198092Srdivacky uint64_t ElementCount = 1; 2469198092Srdivacky do { 2470198092Srdivacky ElementCount *= CA->getSize().getZExtValue(); 2471198092Srdivacky CA = dyn_cast<ConstantArrayType>(CA->getElementType()); 2472198092Srdivacky } while (CA); 2473198092Srdivacky return ElementCount; 2474198092Srdivacky} 2475198092Srdivacky 2476193326Sed/// getFloatingRank - Return a relative rank for floating point types. 2477193326Sed/// This routine will assert if passed a built-in type that isn't a float. 2478193326Sedstatic FloatingRank getFloatingRank(QualType T) { 2479198092Srdivacky if (const ComplexType *CT = T->getAs<ComplexType>()) 2480193326Sed return getFloatingRank(CT->getElementType()); 2481193326Sed 2482198092Srdivacky assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); 2483198092Srdivacky switch (T->getAs<BuiltinType>()->getKind()) { 2484193326Sed default: assert(0 && "getFloatingRank(): not a floating type"); 2485193326Sed case BuiltinType::Float: return FloatRank; 2486193326Sed case BuiltinType::Double: return DoubleRank; 2487193326Sed case BuiltinType::LongDouble: return LongDoubleRank; 2488193326Sed } 2489193326Sed} 2490193326Sed 2491198092Srdivacky/// getFloatingTypeOfSizeWithinDomain - Returns a real floating 2492198092Srdivacky/// point or a complex type (based on typeDomain/typeSize). 2493193326Sed/// 'typeDomain' is a real floating point or complex type. 2494193326Sed/// 'typeSize' is a real floating point or complex type. 2495193326SedQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, 2496193326Sed QualType Domain) const { 2497193326Sed FloatingRank EltRank = getFloatingRank(Size); 2498193326Sed if (Domain->isComplexType()) { 2499193326Sed switch (EltRank) { 2500193326Sed default: assert(0 && "getFloatingRank(): illegal value for rank"); 2501193326Sed case FloatRank: return FloatComplexTy; 2502193326Sed case DoubleRank: return DoubleComplexTy; 2503193326Sed case LongDoubleRank: return LongDoubleComplexTy; 2504193326Sed } 2505193326Sed } 2506193326Sed 2507193326Sed assert(Domain->isRealFloatingType() && "Unknown domain!"); 2508193326Sed switch (EltRank) { 2509193326Sed default: assert(0 && "getFloatingRank(): illegal value for rank"); 2510193326Sed case FloatRank: return FloatTy; 2511193326Sed case DoubleRank: return DoubleTy; 2512193326Sed case LongDoubleRank: return LongDoubleTy; 2513193326Sed } 2514193326Sed} 2515193326Sed 2516193326Sed/// getFloatingTypeOrder - Compare the rank of the two specified floating 2517193326Sed/// point types, ignoring the domain of the type (i.e. 'double' == 2518193326Sed/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 2519198092Srdivacky/// LHS < RHS, return -1. 2520193326Sedint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { 2521193326Sed FloatingRank LHSR = getFloatingRank(LHS); 2522193326Sed FloatingRank RHSR = getFloatingRank(RHS); 2523198092Srdivacky 2524193326Sed if (LHSR == RHSR) 2525193326Sed return 0; 2526193326Sed if (LHSR > RHSR) 2527193326Sed return 1; 2528193326Sed return -1; 2529193326Sed} 2530193326Sed 2531193326Sed/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This 2532193326Sed/// routine will assert if passed a built-in type that isn't an integer or enum, 2533193326Sed/// or if it is not canonicalized. 2534193326Sedunsigned ASTContext::getIntegerRank(Type *T) { 2535198398Srdivacky assert(T->isCanonicalUnqualified() && "T should be canonicalized"); 2536193326Sed if (EnumType* ET = dyn_cast<EnumType>(T)) 2537193326Sed T = ET->getDecl()->getIntegerType().getTypePtr(); 2538193326Sed 2539198092Srdivacky if (T->isSpecificBuiltinType(BuiltinType::WChar)) 2540198092Srdivacky T = getFromTargetType(Target.getWCharType()).getTypePtr(); 2541198092Srdivacky 2542198092Srdivacky if (T->isSpecificBuiltinType(BuiltinType::Char16)) 2543198092Srdivacky T = getFromTargetType(Target.getChar16Type()).getTypePtr(); 2544198092Srdivacky 2545198092Srdivacky if (T->isSpecificBuiltinType(BuiltinType::Char32)) 2546198092Srdivacky T = getFromTargetType(Target.getChar32Type()).getTypePtr(); 2547198092Srdivacky 2548193326Sed // There are two things which impact the integer rank: the width, and 2549193326Sed // the ordering of builtins. The builtin ordering is encoded in the 2550193326Sed // bottom three bits; the width is encoded in the bits above that. 2551194179Sed if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) 2552193326Sed return FWIT->getWidth() << 3; 2553193326Sed 2554193326Sed switch (cast<BuiltinType>(T)->getKind()) { 2555193326Sed default: assert(0 && "getIntegerRank(): not a built-in integer"); 2556193326Sed case BuiltinType::Bool: 2557193326Sed return 1 + (getIntWidth(BoolTy) << 3); 2558193326Sed case BuiltinType::Char_S: 2559193326Sed case BuiltinType::Char_U: 2560193326Sed case BuiltinType::SChar: 2561193326Sed case BuiltinType::UChar: 2562193326Sed return 2 + (getIntWidth(CharTy) << 3); 2563193326Sed case BuiltinType::Short: 2564193326Sed case BuiltinType::UShort: 2565193326Sed return 3 + (getIntWidth(ShortTy) << 3); 2566193326Sed case BuiltinType::Int: 2567193326Sed case BuiltinType::UInt: 2568193326Sed return 4 + (getIntWidth(IntTy) << 3); 2569193326Sed case BuiltinType::Long: 2570193326Sed case BuiltinType::ULong: 2571193326Sed return 5 + (getIntWidth(LongTy) << 3); 2572193326Sed case BuiltinType::LongLong: 2573193326Sed case BuiltinType::ULongLong: 2574193326Sed return 6 + (getIntWidth(LongLongTy) << 3); 2575193326Sed case BuiltinType::Int128: 2576193326Sed case BuiltinType::UInt128: 2577193326Sed return 7 + (getIntWidth(Int128Ty) << 3); 2578193326Sed } 2579193326Sed} 2580193326Sed 2581198092Srdivacky/// \brief Whether this is a promotable bitfield reference according 2582198092Srdivacky/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 2583198092Srdivacky/// 2584198092Srdivacky/// \returns the type this bit-field will promote to, or NULL if no 2585198092Srdivacky/// promotion occurs. 2586198092SrdivackyQualType ASTContext::isPromotableBitField(Expr *E) { 2587198092Srdivacky FieldDecl *Field = E->getBitField(); 2588198092Srdivacky if (!Field) 2589198092Srdivacky return QualType(); 2590198092Srdivacky 2591198092Srdivacky QualType FT = Field->getType(); 2592198092Srdivacky 2593198092Srdivacky llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); 2594198092Srdivacky uint64_t BitWidth = BitWidthAP.getZExtValue(); 2595198092Srdivacky uint64_t IntSize = getTypeSize(IntTy); 2596198092Srdivacky // GCC extension compatibility: if the bit-field size is less than or equal 2597198092Srdivacky // to the size of int, it gets promoted no matter what its type is. 2598198092Srdivacky // For instance, unsigned long bf : 4 gets promoted to signed int. 2599198092Srdivacky if (BitWidth < IntSize) 2600198092Srdivacky return IntTy; 2601198092Srdivacky 2602198092Srdivacky if (BitWidth == IntSize) 2603198092Srdivacky return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; 2604198092Srdivacky 2605198092Srdivacky // Types bigger than int are not subject to promotions, and therefore act 2606198092Srdivacky // like the base type. 2607198092Srdivacky // FIXME: This doesn't quite match what gcc does, but what gcc does here 2608198092Srdivacky // is ridiculous. 2609198092Srdivacky return QualType(); 2610198092Srdivacky} 2611198092Srdivacky 2612198092Srdivacky/// getPromotedIntegerType - Returns the type that Promotable will 2613198092Srdivacky/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 2614198092Srdivacky/// integer type. 2615198092SrdivackyQualType ASTContext::getPromotedIntegerType(QualType Promotable) { 2616198092Srdivacky assert(!Promotable.isNull()); 2617198092Srdivacky assert(Promotable->isPromotableIntegerType()); 2618198092Srdivacky if (Promotable->isSignedIntegerType()) 2619198092Srdivacky return IntTy; 2620198092Srdivacky uint64_t PromotableSize = getTypeSize(Promotable); 2621198092Srdivacky uint64_t IntSize = getTypeSize(IntTy); 2622198092Srdivacky assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); 2623198092Srdivacky return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; 2624198092Srdivacky} 2625198092Srdivacky 2626198092Srdivacky/// getIntegerTypeOrder - Returns the highest ranked integer type: 2627193326Sed/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 2628198092Srdivacky/// LHS < RHS, return -1. 2629193326Sedint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { 2630193326Sed Type *LHSC = getCanonicalType(LHS).getTypePtr(); 2631193326Sed Type *RHSC = getCanonicalType(RHS).getTypePtr(); 2632193326Sed if (LHSC == RHSC) return 0; 2633198092Srdivacky 2634193326Sed bool LHSUnsigned = LHSC->isUnsignedIntegerType(); 2635193326Sed bool RHSUnsigned = RHSC->isUnsignedIntegerType(); 2636198092Srdivacky 2637193326Sed unsigned LHSRank = getIntegerRank(LHSC); 2638193326Sed unsigned RHSRank = getIntegerRank(RHSC); 2639198092Srdivacky 2640193326Sed if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. 2641193326Sed if (LHSRank == RHSRank) return 0; 2642193326Sed return LHSRank > RHSRank ? 1 : -1; 2643193326Sed } 2644198092Srdivacky 2645193326Sed // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. 2646193326Sed if (LHSUnsigned) { 2647193326Sed // If the unsigned [LHS] type is larger, return it. 2648193326Sed if (LHSRank >= RHSRank) 2649193326Sed return 1; 2650198092Srdivacky 2651193326Sed // If the signed type can represent all values of the unsigned type, it 2652193326Sed // wins. Because we are dealing with 2's complement and types that are 2653198092Srdivacky // powers of two larger than each other, this is always safe. 2654193326Sed return -1; 2655193326Sed } 2656193326Sed 2657193326Sed // If the unsigned [RHS] type is larger, return it. 2658193326Sed if (RHSRank >= LHSRank) 2659193326Sed return -1; 2660198092Srdivacky 2661193326Sed // If the signed type can represent all values of the unsigned type, it 2662193326Sed // wins. Because we are dealing with 2's complement and types that are 2663198092Srdivacky // powers of two larger than each other, this is always safe. 2664193326Sed return 1; 2665193326Sed} 2666193326Sed 2667198092Srdivacky// getCFConstantStringType - Return the type used for constant CFStrings. 2668193326SedQualType ASTContext::getCFConstantStringType() { 2669193326Sed if (!CFConstantStringTypeDecl) { 2670198092Srdivacky CFConstantStringTypeDecl = 2671198092Srdivacky RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2672193326Sed &Idents.get("NSConstantString")); 2673193326Sed QualType FieldTypes[4]; 2674198092Srdivacky 2675193326Sed // const int *isa; 2676198092Srdivacky FieldTypes[0] = getPointerType(IntTy.withConst()); 2677193326Sed // int flags; 2678193326Sed FieldTypes[1] = IntTy; 2679193326Sed // const char *str; 2680198092Srdivacky FieldTypes[2] = getPointerType(CharTy.withConst()); 2681193326Sed // long length; 2682198092Srdivacky FieldTypes[3] = LongTy; 2683198092Srdivacky 2684193326Sed // Create fields 2685193326Sed for (unsigned i = 0; i < 4; ++i) { 2686198092Srdivacky FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, 2687193326Sed SourceLocation(), 0, 2688198092Srdivacky FieldTypes[i], /*DInfo=*/0, 2689198092Srdivacky /*BitWidth=*/0, 2690193326Sed /*Mutable=*/false); 2691195341Sed CFConstantStringTypeDecl->addDecl(Field); 2692193326Sed } 2693193326Sed 2694193326Sed CFConstantStringTypeDecl->completeDefinition(*this); 2695193326Sed } 2696198092Srdivacky 2697193326Sed return getTagDeclType(CFConstantStringTypeDecl); 2698193326Sed} 2699193326Sed 2700193326Sedvoid ASTContext::setCFConstantStringType(QualType T) { 2701198092Srdivacky const RecordType *Rec = T->getAs<RecordType>(); 2702193326Sed assert(Rec && "Invalid CFConstantStringType"); 2703193326Sed CFConstantStringTypeDecl = Rec->getDecl(); 2704193326Sed} 2705193326Sed 2706198092SrdivackyQualType ASTContext::getObjCFastEnumerationStateType() { 2707193326Sed if (!ObjCFastEnumerationStateTypeDecl) { 2708193326Sed ObjCFastEnumerationStateTypeDecl = 2709193326Sed RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2710193326Sed &Idents.get("__objcFastEnumerationState")); 2711198092Srdivacky 2712193326Sed QualType FieldTypes[] = { 2713193326Sed UnsignedLongTy, 2714198092Srdivacky getPointerType(ObjCIdTypedefType), 2715193326Sed getPointerType(UnsignedLongTy), 2716193326Sed getConstantArrayType(UnsignedLongTy, 2717193326Sed llvm::APInt(32, 5), ArrayType::Normal, 0) 2718193326Sed }; 2719198092Srdivacky 2720193326Sed for (size_t i = 0; i < 4; ++i) { 2721198092Srdivacky FieldDecl *Field = FieldDecl::Create(*this, 2722198092Srdivacky ObjCFastEnumerationStateTypeDecl, 2723198092Srdivacky SourceLocation(), 0, 2724198092Srdivacky FieldTypes[i], /*DInfo=*/0, 2725198092Srdivacky /*BitWidth=*/0, 2726193326Sed /*Mutable=*/false); 2727195341Sed ObjCFastEnumerationStateTypeDecl->addDecl(Field); 2728193326Sed } 2729198092Srdivacky 2730193326Sed ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); 2731193326Sed } 2732198092Srdivacky 2733193326Sed return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 2734193326Sed} 2735193326Sed 2736198398SrdivackyQualType ASTContext::getBlockDescriptorType() { 2737198398Srdivacky if (BlockDescriptorType) 2738198398Srdivacky return getTagDeclType(BlockDescriptorType); 2739198398Srdivacky 2740198398Srdivacky RecordDecl *T; 2741198398Srdivacky // FIXME: Needs the FlagAppleBlock bit. 2742198398Srdivacky T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2743198398Srdivacky &Idents.get("__block_descriptor")); 2744198398Srdivacky 2745198398Srdivacky QualType FieldTypes[] = { 2746198398Srdivacky UnsignedLongTy, 2747198398Srdivacky UnsignedLongTy, 2748198398Srdivacky }; 2749198398Srdivacky 2750198398Srdivacky const char *FieldNames[] = { 2751198398Srdivacky "reserved", 2752198398Srdivacky "Size" 2753198398Srdivacky }; 2754198398Srdivacky 2755198398Srdivacky for (size_t i = 0; i < 2; ++i) { 2756198398Srdivacky FieldDecl *Field = FieldDecl::Create(*this, 2757198398Srdivacky T, 2758198398Srdivacky SourceLocation(), 2759198398Srdivacky &Idents.get(FieldNames[i]), 2760198398Srdivacky FieldTypes[i], /*DInfo=*/0, 2761198398Srdivacky /*BitWidth=*/0, 2762198398Srdivacky /*Mutable=*/false); 2763198398Srdivacky T->addDecl(Field); 2764198398Srdivacky } 2765198398Srdivacky 2766198398Srdivacky T->completeDefinition(*this); 2767198398Srdivacky 2768198398Srdivacky BlockDescriptorType = T; 2769198398Srdivacky 2770198398Srdivacky return getTagDeclType(BlockDescriptorType); 2771198398Srdivacky} 2772198398Srdivacky 2773198398Srdivackyvoid ASTContext::setBlockDescriptorType(QualType T) { 2774198398Srdivacky const RecordType *Rec = T->getAs<RecordType>(); 2775198398Srdivacky assert(Rec && "Invalid BlockDescriptorType"); 2776198398Srdivacky BlockDescriptorType = Rec->getDecl(); 2777198398Srdivacky} 2778198398Srdivacky 2779198398SrdivackyQualType ASTContext::getBlockDescriptorExtendedType() { 2780198398Srdivacky if (BlockDescriptorExtendedType) 2781198398Srdivacky return getTagDeclType(BlockDescriptorExtendedType); 2782198398Srdivacky 2783198398Srdivacky RecordDecl *T; 2784198398Srdivacky // FIXME: Needs the FlagAppleBlock bit. 2785198398Srdivacky T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2786198398Srdivacky &Idents.get("__block_descriptor_withcopydispose")); 2787198398Srdivacky 2788198398Srdivacky QualType FieldTypes[] = { 2789198398Srdivacky UnsignedLongTy, 2790198398Srdivacky UnsignedLongTy, 2791198398Srdivacky getPointerType(VoidPtrTy), 2792198398Srdivacky getPointerType(VoidPtrTy) 2793198398Srdivacky }; 2794198398Srdivacky 2795198398Srdivacky const char *FieldNames[] = { 2796198398Srdivacky "reserved", 2797198398Srdivacky "Size", 2798198398Srdivacky "CopyFuncPtr", 2799198398Srdivacky "DestroyFuncPtr" 2800198398Srdivacky }; 2801198398Srdivacky 2802198398Srdivacky for (size_t i = 0; i < 4; ++i) { 2803198398Srdivacky FieldDecl *Field = FieldDecl::Create(*this, 2804198398Srdivacky T, 2805198398Srdivacky SourceLocation(), 2806198398Srdivacky &Idents.get(FieldNames[i]), 2807198398Srdivacky FieldTypes[i], /*DInfo=*/0, 2808198398Srdivacky /*BitWidth=*/0, 2809198398Srdivacky /*Mutable=*/false); 2810198398Srdivacky T->addDecl(Field); 2811198398Srdivacky } 2812198398Srdivacky 2813198398Srdivacky T->completeDefinition(*this); 2814198398Srdivacky 2815198398Srdivacky BlockDescriptorExtendedType = T; 2816198398Srdivacky 2817198398Srdivacky return getTagDeclType(BlockDescriptorExtendedType); 2818198398Srdivacky} 2819198398Srdivacky 2820198398Srdivackyvoid ASTContext::setBlockDescriptorExtendedType(QualType T) { 2821198398Srdivacky const RecordType *Rec = T->getAs<RecordType>(); 2822198398Srdivacky assert(Rec && "Invalid BlockDescriptorType"); 2823198398Srdivacky BlockDescriptorExtendedType = Rec->getDecl(); 2824198398Srdivacky} 2825198398Srdivacky 2826198398Srdivackybool ASTContext::BlockRequiresCopying(QualType Ty) { 2827198398Srdivacky if (Ty->isBlockPointerType()) 2828198398Srdivacky return true; 2829198398Srdivacky if (isObjCNSObjectType(Ty)) 2830198398Srdivacky return true; 2831198398Srdivacky if (Ty->isObjCObjectPointerType()) 2832198398Srdivacky return true; 2833198398Srdivacky return false; 2834198398Srdivacky} 2835198398Srdivacky 2836198398SrdivackyQualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { 2837198398Srdivacky // type = struct __Block_byref_1_X { 2838198398Srdivacky // void *__isa; 2839198398Srdivacky // struct __Block_byref_1_X *__forwarding; 2840198398Srdivacky // unsigned int __flags; 2841198398Srdivacky // unsigned int __size; 2842198398Srdivacky // void *__copy_helper; // as needed 2843198398Srdivacky // void *__destroy_help // as needed 2844198398Srdivacky // int X; 2845198398Srdivacky // } * 2846198398Srdivacky 2847198398Srdivacky bool HasCopyAndDispose = BlockRequiresCopying(Ty); 2848198398Srdivacky 2849198398Srdivacky // FIXME: Move up 2850198398Srdivacky static int UniqueBlockByRefTypeID = 0; 2851198398Srdivacky char Name[36]; 2852198398Srdivacky sprintf(Name, "__Block_byref_%d_%s", ++UniqueBlockByRefTypeID, DeclName); 2853198398Srdivacky RecordDecl *T; 2854198398Srdivacky T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2855198398Srdivacky &Idents.get(Name)); 2856198398Srdivacky T->startDefinition(); 2857198398Srdivacky QualType Int32Ty = IntTy; 2858198398Srdivacky assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); 2859198398Srdivacky QualType FieldTypes[] = { 2860198398Srdivacky getPointerType(VoidPtrTy), 2861198398Srdivacky getPointerType(getTagDeclType(T)), 2862198398Srdivacky Int32Ty, 2863198398Srdivacky Int32Ty, 2864198398Srdivacky getPointerType(VoidPtrTy), 2865198398Srdivacky getPointerType(VoidPtrTy), 2866198398Srdivacky Ty 2867198398Srdivacky }; 2868198398Srdivacky 2869198398Srdivacky const char *FieldNames[] = { 2870198398Srdivacky "__isa", 2871198398Srdivacky "__forwarding", 2872198398Srdivacky "__flags", 2873198398Srdivacky "__size", 2874198398Srdivacky "__copy_helper", 2875198398Srdivacky "__destroy_helper", 2876198398Srdivacky DeclName, 2877198398Srdivacky }; 2878198398Srdivacky 2879198398Srdivacky for (size_t i = 0; i < 7; ++i) { 2880198398Srdivacky if (!HasCopyAndDispose && i >=4 && i <= 5) 2881198398Srdivacky continue; 2882198398Srdivacky FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), 2883198398Srdivacky &Idents.get(FieldNames[i]), 2884198398Srdivacky FieldTypes[i], /*DInfo=*/0, 2885198398Srdivacky /*BitWidth=*/0, /*Mutable=*/false); 2886198398Srdivacky T->addDecl(Field); 2887198398Srdivacky } 2888198398Srdivacky 2889198398Srdivacky T->completeDefinition(*this); 2890198398Srdivacky 2891198398Srdivacky return getPointerType(getTagDeclType(T)); 2892198398Srdivacky} 2893198398Srdivacky 2894198398Srdivacky 2895198398SrdivackyQualType ASTContext::getBlockParmType( 2896198398Srdivacky bool BlockHasCopyDispose, 2897198398Srdivacky llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { 2898198398Srdivacky // FIXME: Move up 2899198398Srdivacky static int UniqueBlockParmTypeID = 0; 2900198398Srdivacky char Name[36]; 2901198398Srdivacky sprintf(Name, "__block_literal_%u", ++UniqueBlockParmTypeID); 2902198398Srdivacky RecordDecl *T; 2903198398Srdivacky T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2904198398Srdivacky &Idents.get(Name)); 2905198398Srdivacky QualType FieldTypes[] = { 2906198398Srdivacky getPointerType(VoidPtrTy), 2907198398Srdivacky IntTy, 2908198398Srdivacky IntTy, 2909198398Srdivacky getPointerType(VoidPtrTy), 2910198398Srdivacky (BlockHasCopyDispose ? 2911198398Srdivacky getPointerType(getBlockDescriptorExtendedType()) : 2912198398Srdivacky getPointerType(getBlockDescriptorType())) 2913198398Srdivacky }; 2914198398Srdivacky 2915198398Srdivacky const char *FieldNames[] = { 2916198398Srdivacky "__isa", 2917198398Srdivacky "__flags", 2918198398Srdivacky "__reserved", 2919198398Srdivacky "__FuncPtr", 2920198398Srdivacky "__descriptor" 2921198398Srdivacky }; 2922198398Srdivacky 2923198398Srdivacky for (size_t i = 0; i < 5; ++i) { 2924198398Srdivacky FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), 2925198398Srdivacky &Idents.get(FieldNames[i]), 2926198398Srdivacky FieldTypes[i], /*DInfo=*/0, 2927198398Srdivacky /*BitWidth=*/0, /*Mutable=*/false); 2928198398Srdivacky T->addDecl(Field); 2929198398Srdivacky } 2930198398Srdivacky 2931198398Srdivacky for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { 2932198398Srdivacky const Expr *E = BlockDeclRefDecls[i]; 2933198398Srdivacky const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); 2934198398Srdivacky clang::IdentifierInfo *Name = 0; 2935198398Srdivacky if (BDRE) { 2936198398Srdivacky const ValueDecl *D = BDRE->getDecl(); 2937198398Srdivacky Name = &Idents.get(D->getName()); 2938198398Srdivacky } 2939198398Srdivacky QualType FieldType = E->getType(); 2940198398Srdivacky 2941198398Srdivacky if (BDRE && BDRE->isByRef()) 2942198398Srdivacky FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), 2943198398Srdivacky FieldType); 2944198398Srdivacky 2945198398Srdivacky FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), 2946198398Srdivacky Name, FieldType, /*DInfo=*/0, 2947198398Srdivacky /*BitWidth=*/0, /*Mutable=*/false); 2948198398Srdivacky T->addDecl(Field); 2949198398Srdivacky } 2950198398Srdivacky 2951198398Srdivacky T->completeDefinition(*this); 2952198398Srdivacky 2953198398Srdivacky return getPointerType(getTagDeclType(T)); 2954198398Srdivacky} 2955198398Srdivacky 2956193326Sedvoid ASTContext::setObjCFastEnumerationStateType(QualType T) { 2957198092Srdivacky const RecordType *Rec = T->getAs<RecordType>(); 2958193326Sed assert(Rec && "Invalid ObjCFAstEnumerationStateType"); 2959193326Sed ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); 2960193326Sed} 2961193326Sed 2962193326Sed// This returns true if a type has been typedefed to BOOL: 2963193326Sed// typedef <type> BOOL; 2964193326Sedstatic bool isTypeTypedefedAsBOOL(QualType T) { 2965193326Sed if (const TypedefType *TT = dyn_cast<TypedefType>(T)) 2966193326Sed if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) 2967193326Sed return II->isStr("BOOL"); 2968198092Srdivacky 2969193326Sed return false; 2970193326Sed} 2971193326Sed 2972193326Sed/// getObjCEncodingTypeSize returns size of type for objective-c encoding 2973193326Sed/// purpose. 2974193326Sedint ASTContext::getObjCEncodingTypeSize(QualType type) { 2975193326Sed uint64_t sz = getTypeSize(type); 2976198092Srdivacky 2977193326Sed // Make all integer and enum types at least as large as an int 2978193326Sed if (sz > 0 && type->isIntegralType()) 2979193326Sed sz = std::max(sz, getTypeSize(IntTy)); 2980193326Sed // Treat arrays as pointers, since that's how they're passed in. 2981193326Sed else if (type->isArrayType()) 2982193326Sed sz = getTypeSize(VoidPtrTy); 2983193326Sed return sz / getTypeSize(CharTy); 2984193326Sed} 2985193326Sed 2986193326Sed/// getObjCEncodingForMethodDecl - Return the encoded type for this method 2987193326Sed/// declaration. 2988198092Srdivackyvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, 2989193326Sed std::string& S) { 2990193326Sed // FIXME: This is not very efficient. 2991193326Sed // Encode type qualifer, 'in', 'inout', etc. for the return type. 2992193326Sed getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); 2993193326Sed // Encode result type. 2994193326Sed getObjCEncodingForType(Decl->getResultType(), S); 2995193326Sed // Compute size of all parameters. 2996193326Sed // Start with computing size of a pointer in number of bytes. 2997193326Sed // FIXME: There might(should) be a better way of doing this computation! 2998193326Sed SourceLocation Loc; 2999193326Sed int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); 3000193326Sed // The first two arguments (self and _cmd) are pointers; account for 3001193326Sed // their size. 3002193326Sed int ParmOffset = 2 * PtrSize; 3003193326Sed for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 3004193326Sed E = Decl->param_end(); PI != E; ++PI) { 3005193326Sed QualType PType = (*PI)->getType(); 3006193326Sed int sz = getObjCEncodingTypeSize(PType); 3007193326Sed assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); 3008193326Sed ParmOffset += sz; 3009193326Sed } 3010193326Sed S += llvm::utostr(ParmOffset); 3011193326Sed S += "@0:"; 3012193326Sed S += llvm::utostr(PtrSize); 3013198092Srdivacky 3014193326Sed // Argument types. 3015193326Sed ParmOffset = 2 * PtrSize; 3016193326Sed for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 3017193326Sed E = Decl->param_end(); PI != E; ++PI) { 3018193326Sed ParmVarDecl *PVDecl = *PI; 3019198092Srdivacky QualType PType = PVDecl->getOriginalType(); 3020193326Sed if (const ArrayType *AT = 3021193326Sed dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { 3022193326Sed // Use array's original type only if it has known number of 3023193326Sed // elements. 3024193326Sed if (!isa<ConstantArrayType>(AT)) 3025193326Sed PType = PVDecl->getType(); 3026193326Sed } else if (PType->isFunctionType()) 3027193326Sed PType = PVDecl->getType(); 3028193326Sed // Process argument qualifiers for user supplied arguments; such as, 3029193326Sed // 'in', 'inout', etc. 3030193326Sed getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); 3031193326Sed getObjCEncodingForType(PType, S); 3032193326Sed S += llvm::utostr(ParmOffset); 3033193326Sed ParmOffset += getObjCEncodingTypeSize(PType); 3034193326Sed } 3035193326Sed} 3036193326Sed 3037193326Sed/// getObjCEncodingForPropertyDecl - Return the encoded type for this 3038193326Sed/// property declaration. If non-NULL, Container must be either an 3039193326Sed/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be 3040193326Sed/// NULL when getting encodings for protocol properties. 3041198092Srdivacky/// Property attributes are stored as a comma-delimited C string. The simple 3042198092Srdivacky/// attributes readonly and bycopy are encoded as single characters. The 3043198092Srdivacky/// parametrized attributes, getter=name, setter=name, and ivar=name, are 3044198092Srdivacky/// encoded as single characters, followed by an identifier. Property types 3045198092Srdivacky/// are also encoded as a parametrized attribute. The characters used to encode 3046193326Sed/// these attributes are defined by the following enumeration: 3047193326Sed/// @code 3048193326Sed/// enum PropertyAttributes { 3049193326Sed/// kPropertyReadOnly = 'R', // property is read-only. 3050193326Sed/// kPropertyBycopy = 'C', // property is a copy of the value last assigned 3051193326Sed/// kPropertyByref = '&', // property is a reference to the value last assigned 3052193326Sed/// kPropertyDynamic = 'D', // property is dynamic 3053193326Sed/// kPropertyGetter = 'G', // followed by getter selector name 3054193326Sed/// kPropertySetter = 'S', // followed by setter selector name 3055193326Sed/// kPropertyInstanceVariable = 'V' // followed by instance variable name 3056193326Sed/// kPropertyType = 't' // followed by old-style type encoding. 3057193326Sed/// kPropertyWeak = 'W' // 'weak' property 3058193326Sed/// kPropertyStrong = 'P' // property GC'able 3059193326Sed/// kPropertyNonAtomic = 'N' // property non-atomic 3060193326Sed/// }; 3061193326Sed/// @endcode 3062198092Srdivackyvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 3063193326Sed const Decl *Container, 3064193326Sed std::string& S) { 3065193326Sed // Collect information from the property implementation decl(s). 3066193326Sed bool Dynamic = false; 3067193326Sed ObjCPropertyImplDecl *SynthesizePID = 0; 3068193326Sed 3069193326Sed // FIXME: Duplicated code due to poor abstraction. 3070193326Sed if (Container) { 3071198092Srdivacky if (const ObjCCategoryImplDecl *CID = 3072193326Sed dyn_cast<ObjCCategoryImplDecl>(Container)) { 3073193326Sed for (ObjCCategoryImplDecl::propimpl_iterator 3074195341Sed i = CID->propimpl_begin(), e = CID->propimpl_end(); 3075193326Sed i != e; ++i) { 3076193326Sed ObjCPropertyImplDecl *PID = *i; 3077193326Sed if (PID->getPropertyDecl() == PD) { 3078193326Sed if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 3079193326Sed Dynamic = true; 3080193326Sed } else { 3081193326Sed SynthesizePID = PID; 3082193326Sed } 3083193326Sed } 3084193326Sed } 3085193326Sed } else { 3086193326Sed const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); 3087193326Sed for (ObjCCategoryImplDecl::propimpl_iterator 3088195341Sed i = OID->propimpl_begin(), e = OID->propimpl_end(); 3089193326Sed i != e; ++i) { 3090193326Sed ObjCPropertyImplDecl *PID = *i; 3091193326Sed if (PID->getPropertyDecl() == PD) { 3092193326Sed if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 3093193326Sed Dynamic = true; 3094193326Sed } else { 3095193326Sed SynthesizePID = PID; 3096193326Sed } 3097193326Sed } 3098198092Srdivacky } 3099193326Sed } 3100193326Sed } 3101193326Sed 3102193326Sed // FIXME: This is not very efficient. 3103193326Sed S = "T"; 3104193326Sed 3105193326Sed // Encode result type. 3106193326Sed // GCC has some special rules regarding encoding of properties which 3107193326Sed // closely resembles encoding of ivars. 3108198092Srdivacky getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, 3109193326Sed true /* outermost type */, 3110193326Sed true /* encoding for property */); 3111193326Sed 3112193326Sed if (PD->isReadOnly()) { 3113193326Sed S += ",R"; 3114193326Sed } else { 3115193326Sed switch (PD->getSetterKind()) { 3116193326Sed case ObjCPropertyDecl::Assign: break; 3117193326Sed case ObjCPropertyDecl::Copy: S += ",C"; break; 3118198092Srdivacky case ObjCPropertyDecl::Retain: S += ",&"; break; 3119193326Sed } 3120193326Sed } 3121193326Sed 3122193326Sed // It really isn't clear at all what this means, since properties 3123193326Sed // are "dynamic by default". 3124193326Sed if (Dynamic) 3125193326Sed S += ",D"; 3126193326Sed 3127193326Sed if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) 3128193326Sed S += ",N"; 3129198092Srdivacky 3130193326Sed if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 3131193326Sed S += ",G"; 3132193326Sed S += PD->getGetterName().getAsString(); 3133193326Sed } 3134193326Sed 3135193326Sed if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 3136193326Sed S += ",S"; 3137193326Sed S += PD->getSetterName().getAsString(); 3138193326Sed } 3139193326Sed 3140193326Sed if (SynthesizePID) { 3141193326Sed const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); 3142193326Sed S += ",V"; 3143193326Sed S += OID->getNameAsString(); 3144193326Sed } 3145193326Sed 3146193326Sed // FIXME: OBJCGC: weak & strong 3147193326Sed} 3148193326Sed 3149193326Sed/// getLegacyIntegralTypeEncoding - 3150198092Srdivacky/// Another legacy compatibility encoding: 32-bit longs are encoded as 3151198092Srdivacky/// 'l' or 'L' , but not always. For typedefs, we need to use 3152193326Sed/// 'i' or 'I' instead if encoding a struct field, or a pointer! 3153193326Sed/// 3154193326Sedvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { 3155198092Srdivacky if (isa<TypedefType>(PointeeTy.getTypePtr())) { 3156198092Srdivacky if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { 3157193326Sed if (BT->getKind() == BuiltinType::ULong && 3158193326Sed ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 3159193326Sed PointeeTy = UnsignedIntTy; 3160198092Srdivacky else 3161193326Sed if (BT->getKind() == BuiltinType::Long && 3162193326Sed ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 3163193326Sed PointeeTy = IntTy; 3164193326Sed } 3165193326Sed } 3166193326Sed} 3167193326Sed 3168193326Sedvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S, 3169193326Sed const FieldDecl *Field) { 3170193326Sed // We follow the behavior of gcc, expanding structures which are 3171193326Sed // directly pointed to, and expanding embedded structures. Note that 3172193326Sed // these rules are sufficient to prevent recursive encoding of the 3173193326Sed // same type. 3174198092Srdivacky getObjCEncodingForTypeImpl(T, S, true, true, Field, 3175193326Sed true /* outermost type */); 3176193326Sed} 3177193326Sed 3178198092Srdivackystatic void EncodeBitField(const ASTContext *Context, std::string& S, 3179193326Sed const FieldDecl *FD) { 3180193326Sed const Expr *E = FD->getBitWidth(); 3181193326Sed assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); 3182193326Sed ASTContext *Ctx = const_cast<ASTContext*>(Context); 3183193326Sed unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); 3184193326Sed S += 'b'; 3185193326Sed S += llvm::utostr(N); 3186193326Sed} 3187193326Sed 3188198398Srdivacky// FIXME: Use SmallString for accumulating string. 3189193326Sedvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, 3190193326Sed bool ExpandPointedToStructures, 3191193326Sed bool ExpandStructures, 3192193326Sed const FieldDecl *FD, 3193193326Sed bool OutermostType, 3194193326Sed bool EncodingProperty) { 3195198092Srdivacky if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 3196198092Srdivacky if (FD && FD->isBitField()) 3197198092Srdivacky return EncodeBitField(this, S, FD); 3198198092Srdivacky char encoding; 3199198092Srdivacky switch (BT->getKind()) { 3200198092Srdivacky default: assert(0 && "Unhandled builtin type kind"); 3201198092Srdivacky case BuiltinType::Void: encoding = 'v'; break; 3202198092Srdivacky case BuiltinType::Bool: encoding = 'B'; break; 3203198092Srdivacky case BuiltinType::Char_U: 3204198092Srdivacky case BuiltinType::UChar: encoding = 'C'; break; 3205198092Srdivacky case BuiltinType::UShort: encoding = 'S'; break; 3206198092Srdivacky case BuiltinType::UInt: encoding = 'I'; break; 3207198092Srdivacky case BuiltinType::ULong: 3208198092Srdivacky encoding = 3209198092Srdivacky (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; 3210193326Sed break; 3211198092Srdivacky case BuiltinType::UInt128: encoding = 'T'; break; 3212198092Srdivacky case BuiltinType::ULongLong: encoding = 'Q'; break; 3213198092Srdivacky case BuiltinType::Char_S: 3214198092Srdivacky case BuiltinType::SChar: encoding = 'c'; break; 3215198092Srdivacky case BuiltinType::Short: encoding = 's'; break; 3216198092Srdivacky case BuiltinType::Int: encoding = 'i'; break; 3217198092Srdivacky case BuiltinType::Long: 3218198092Srdivacky encoding = 3219198092Srdivacky (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; 3220198092Srdivacky break; 3221198092Srdivacky case BuiltinType::LongLong: encoding = 'q'; break; 3222198092Srdivacky case BuiltinType::Int128: encoding = 't'; break; 3223198092Srdivacky case BuiltinType::Float: encoding = 'f'; break; 3224198092Srdivacky case BuiltinType::Double: encoding = 'd'; break; 3225198092Srdivacky case BuiltinType::LongDouble: encoding = 'd'; break; 3226193326Sed } 3227198092Srdivacky 3228198092Srdivacky S += encoding; 3229198092Srdivacky return; 3230198092Srdivacky } 3231198092Srdivacky 3232198092Srdivacky if (const ComplexType *CT = T->getAs<ComplexType>()) { 3233193326Sed S += 'j'; 3234198092Srdivacky getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, 3235193326Sed false); 3236193326Sed return; 3237193326Sed } 3238198092Srdivacky 3239198092Srdivacky if (const PointerType *PT = T->getAs<PointerType>()) { 3240193326Sed QualType PointeeTy = PT->getPointeeType(); 3241193326Sed bool isReadOnly = false; 3242193326Sed // For historical/compatibility reasons, the read-only qualifier of the 3243193326Sed // pointee gets emitted _before_ the '^'. The read-only qualifier of 3244193326Sed // the pointer itself gets ignored, _unless_ we are looking at a typedef! 3245198092Srdivacky // Also, do not emit the 'r' for anything but the outermost type! 3246198092Srdivacky if (isa<TypedefType>(T.getTypePtr())) { 3247193326Sed if (OutermostType && T.isConstQualified()) { 3248193326Sed isReadOnly = true; 3249193326Sed S += 'r'; 3250193326Sed } 3251198092Srdivacky } else if (OutermostType) { 3252193326Sed QualType P = PointeeTy; 3253198092Srdivacky while (P->getAs<PointerType>()) 3254198092Srdivacky P = P->getAs<PointerType>()->getPointeeType(); 3255193326Sed if (P.isConstQualified()) { 3256193326Sed isReadOnly = true; 3257193326Sed S += 'r'; 3258193326Sed } 3259193326Sed } 3260193326Sed if (isReadOnly) { 3261193326Sed // Another legacy compatibility encoding. Some ObjC qualifier and type 3262193326Sed // combinations need to be rearranged. 3263193326Sed // Rewrite "in const" from "nr" to "rn" 3264193326Sed const char * s = S.c_str(); 3265193326Sed int len = S.length(); 3266193326Sed if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { 3267193326Sed std::string replace = "rn"; 3268193326Sed S.replace(S.end()-2, S.end(), replace); 3269193326Sed } 3270193326Sed } 3271198092Srdivacky if (isObjCSelType(PointeeTy)) { 3272193326Sed S += ':'; 3273193326Sed return; 3274193326Sed } 3275198092Srdivacky 3276193326Sed if (PointeeTy->isCharType()) { 3277193326Sed // char pointer types should be encoded as '*' unless it is a 3278193326Sed // type that has been typedef'd to 'BOOL'. 3279193326Sed if (!isTypeTypedefedAsBOOL(PointeeTy)) { 3280193326Sed S += '*'; 3281193326Sed return; 3282193326Sed } 3283198092Srdivacky } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { 3284198092Srdivacky // GCC binary compat: Need to convert "struct objc_class *" to "#". 3285198092Srdivacky if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { 3286198092Srdivacky S += '#'; 3287198092Srdivacky return; 3288198092Srdivacky } 3289198092Srdivacky // GCC binary compat: Need to convert "struct objc_object *" to "@". 3290198092Srdivacky if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { 3291198092Srdivacky S += '@'; 3292198092Srdivacky return; 3293198092Srdivacky } 3294198092Srdivacky // fall through... 3295193326Sed } 3296193326Sed S += '^'; 3297193326Sed getLegacyIntegralTypeEncoding(PointeeTy); 3298193326Sed 3299198092Srdivacky getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, 3300193326Sed NULL); 3301198092Srdivacky return; 3302198092Srdivacky } 3303198092Srdivacky 3304198092Srdivacky if (const ArrayType *AT = 3305198092Srdivacky // Ignore type qualifiers etc. 3306198092Srdivacky dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { 3307193326Sed if (isa<IncompleteArrayType>(AT)) { 3308193326Sed // Incomplete arrays are encoded as a pointer to the array element. 3309193326Sed S += '^'; 3310193326Sed 3311198092Srdivacky getObjCEncodingForTypeImpl(AT->getElementType(), S, 3312193326Sed false, ExpandStructures, FD); 3313193326Sed } else { 3314193326Sed S += '['; 3315198092Srdivacky 3316193326Sed if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 3317193326Sed S += llvm::utostr(CAT->getSize().getZExtValue()); 3318193326Sed else { 3319193326Sed //Variable length arrays are encoded as a regular array with 0 elements. 3320193326Sed assert(isa<VariableArrayType>(AT) && "Unknown array type!"); 3321193326Sed S += '0'; 3322193326Sed } 3323198092Srdivacky 3324198092Srdivacky getObjCEncodingForTypeImpl(AT->getElementType(), S, 3325193326Sed false, ExpandStructures, FD); 3326193326Sed S += ']'; 3327193326Sed } 3328198092Srdivacky return; 3329198092Srdivacky } 3330198092Srdivacky 3331198092Srdivacky if (T->getAs<FunctionType>()) { 3332193326Sed S += '?'; 3333198092Srdivacky return; 3334198092Srdivacky } 3335198092Srdivacky 3336198092Srdivacky if (const RecordType *RTy = T->getAs<RecordType>()) { 3337193326Sed RecordDecl *RDecl = RTy->getDecl(); 3338193326Sed S += RDecl->isUnion() ? '(' : '{'; 3339193326Sed // Anonymous structures print as '?' 3340193326Sed if (const IdentifierInfo *II = RDecl->getIdentifier()) { 3341193326Sed S += II->getName(); 3342193326Sed } else { 3343193326Sed S += '?'; 3344193326Sed } 3345193326Sed if (ExpandStructures) { 3346193326Sed S += '='; 3347195341Sed for (RecordDecl::field_iterator Field = RDecl->field_begin(), 3348195341Sed FieldEnd = RDecl->field_end(); 3349193326Sed Field != FieldEnd; ++Field) { 3350193326Sed if (FD) { 3351193326Sed S += '"'; 3352193326Sed S += Field->getNameAsString(); 3353193326Sed S += '"'; 3354193326Sed } 3355198092Srdivacky 3356193326Sed // Special case bit-fields. 3357193326Sed if (Field->isBitField()) { 3358198092Srdivacky getObjCEncodingForTypeImpl(Field->getType(), S, false, true, 3359193326Sed (*Field)); 3360193326Sed } else { 3361193326Sed QualType qt = Field->getType(); 3362193326Sed getLegacyIntegralTypeEncoding(qt); 3363198092Srdivacky getObjCEncodingForTypeImpl(qt, S, false, true, 3364193326Sed FD); 3365193326Sed } 3366193326Sed } 3367193326Sed } 3368193326Sed S += RDecl->isUnion() ? ')' : '}'; 3369198092Srdivacky return; 3370198092Srdivacky } 3371198092Srdivacky 3372198092Srdivacky if (T->isEnumeralType()) { 3373193326Sed if (FD && FD->isBitField()) 3374193326Sed EncodeBitField(this, S, FD); 3375193326Sed else 3376193326Sed S += 'i'; 3377198092Srdivacky return; 3378198092Srdivacky } 3379198092Srdivacky 3380198092Srdivacky if (T->isBlockPointerType()) { 3381193326Sed S += "@?"; // Unlike a pointer-to-function, which is "^?". 3382198092Srdivacky return; 3383198092Srdivacky } 3384198092Srdivacky 3385198092Srdivacky if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { 3386193326Sed // @encode(class_name) 3387198092Srdivacky ObjCInterfaceDecl *OI = OIT->getDecl(); 3388193326Sed S += '{'; 3389193326Sed const IdentifierInfo *II = OI->getIdentifier(); 3390193326Sed S += II->getName(); 3391193326Sed S += '='; 3392193326Sed llvm::SmallVector<FieldDecl*, 32> RecFields; 3393193326Sed CollectObjCIvars(OI, RecFields); 3394193326Sed for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { 3395193326Sed if (RecFields[i]->isBitField()) 3396198092Srdivacky getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 3397193326Sed RecFields[i]); 3398193326Sed else 3399198092Srdivacky getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 3400193326Sed FD); 3401193326Sed } 3402193326Sed S += '}'; 3403198092Srdivacky return; 3404193326Sed } 3405198092Srdivacky 3406198092Srdivacky if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { 3407198092Srdivacky if (OPT->isObjCIdType()) { 3408198092Srdivacky S += '@'; 3409198092Srdivacky return; 3410198092Srdivacky } 3411198092Srdivacky 3412198092Srdivacky if (OPT->isObjCClassType()) { 3413198092Srdivacky S += '#'; 3414198092Srdivacky return; 3415198092Srdivacky } 3416198092Srdivacky 3417198092Srdivacky if (OPT->isObjCQualifiedIdType()) { 3418198092Srdivacky getObjCEncodingForTypeImpl(getObjCIdType(), S, 3419198092Srdivacky ExpandPointedToStructures, 3420198092Srdivacky ExpandStructures, FD); 3421198092Srdivacky if (FD || EncodingProperty) { 3422198092Srdivacky // Note that we do extended encoding of protocol qualifer list 3423198092Srdivacky // Only when doing ivar or property encoding. 3424198092Srdivacky S += '"'; 3425198092Srdivacky for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 3426198092Srdivacky E = OPT->qual_end(); I != E; ++I) { 3427198092Srdivacky S += '<'; 3428198092Srdivacky S += (*I)->getNameAsString(); 3429198092Srdivacky S += '>'; 3430198092Srdivacky } 3431198092Srdivacky S += '"'; 3432198092Srdivacky } 3433198092Srdivacky return; 3434198092Srdivacky } 3435198092Srdivacky 3436198092Srdivacky QualType PointeeTy = OPT->getPointeeType(); 3437198092Srdivacky if (!EncodingProperty && 3438198092Srdivacky isa<TypedefType>(PointeeTy.getTypePtr())) { 3439198092Srdivacky // Another historical/compatibility reason. 3440198092Srdivacky // We encode the underlying type which comes out as 3441198092Srdivacky // {...}; 3442198092Srdivacky S += '^'; 3443198092Srdivacky getObjCEncodingForTypeImpl(PointeeTy, S, 3444198092Srdivacky false, ExpandPointedToStructures, 3445198092Srdivacky NULL); 3446198092Srdivacky return; 3447198092Srdivacky } 3448198092Srdivacky 3449198092Srdivacky S += '@'; 3450198092Srdivacky if (FD || EncodingProperty) { 3451198092Srdivacky S += '"'; 3452198092Srdivacky S += OPT->getInterfaceDecl()->getNameAsCString(); 3453198092Srdivacky for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 3454198092Srdivacky E = OPT->qual_end(); I != E; ++I) { 3455198092Srdivacky S += '<'; 3456198092Srdivacky S += (*I)->getNameAsString(); 3457198092Srdivacky S += '>'; 3458198092Srdivacky } 3459198092Srdivacky S += '"'; 3460198092Srdivacky } 3461198092Srdivacky return; 3462198092Srdivacky } 3463198092Srdivacky 3464198092Srdivacky assert(0 && "@encode for type not implemented!"); 3465193326Sed} 3466193326Sed 3467198092Srdivackyvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 3468193326Sed std::string& S) const { 3469193326Sed if (QT & Decl::OBJC_TQ_In) 3470193326Sed S += 'n'; 3471193326Sed if (QT & Decl::OBJC_TQ_Inout) 3472193326Sed S += 'N'; 3473193326Sed if (QT & Decl::OBJC_TQ_Out) 3474193326Sed S += 'o'; 3475193326Sed if (QT & Decl::OBJC_TQ_Bycopy) 3476193326Sed S += 'O'; 3477193326Sed if (QT & Decl::OBJC_TQ_Byref) 3478193326Sed S += 'R'; 3479193326Sed if (QT & Decl::OBJC_TQ_Oneway) 3480193326Sed S += 'V'; 3481193326Sed} 3482193326Sed 3483198092Srdivackyvoid ASTContext::setBuiltinVaListType(QualType T) { 3484193326Sed assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); 3485198092Srdivacky 3486193326Sed BuiltinVaListType = T; 3487193326Sed} 3488193326Sed 3489198092Srdivackyvoid ASTContext::setObjCIdType(QualType T) { 3490198092Srdivacky ObjCIdTypedefType = T; 3491193326Sed} 3492193326Sed 3493198092Srdivackyvoid ASTContext::setObjCSelType(QualType T) { 3494193326Sed ObjCSelType = T; 3495193326Sed 3496198092Srdivacky const TypedefType *TT = T->getAs<TypedefType>(); 3497193326Sed if (!TT) 3498193326Sed return; 3499193326Sed TypedefDecl *TD = TT->getDecl(); 3500193326Sed 3501193326Sed // typedef struct objc_selector *SEL; 3502198092Srdivacky const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>(); 3503193326Sed if (!ptr) 3504193326Sed return; 3505193326Sed const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 3506193326Sed if (!rec) 3507193326Sed return; 3508193326Sed SelStructType = rec; 3509193326Sed} 3510193326Sed 3511198092Srdivackyvoid ASTContext::setObjCProtoType(QualType QT) { 3512193326Sed ObjCProtoType = QT; 3513193326Sed} 3514193326Sed 3515198092Srdivackyvoid ASTContext::setObjCClassType(QualType T) { 3516198092Srdivacky ObjCClassTypedefType = T; 3517193326Sed} 3518193326Sed 3519193326Sedvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { 3520198092Srdivacky assert(ObjCConstantStringType.isNull() && 3521193326Sed "'NSConstantString' type already set!"); 3522198092Srdivacky 3523193326Sed ObjCConstantStringType = getObjCInterfaceType(Decl); 3524193326Sed} 3525193326Sed 3526193326Sed/// \brief Retrieve the template name that represents a qualified 3527193326Sed/// template name such as \c std::vector. 3528198092SrdivackyTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 3529193326Sed bool TemplateKeyword, 3530193326Sed TemplateDecl *Template) { 3531193326Sed llvm::FoldingSetNodeID ID; 3532193326Sed QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); 3533193326Sed 3534193326Sed void *InsertPos = 0; 3535193326Sed QualifiedTemplateName *QTN = 3536193326Sed QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 3537193326Sed if (!QTN) { 3538193326Sed QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); 3539193326Sed QualifiedTemplateNames.InsertNode(QTN, InsertPos); 3540193326Sed } 3541193326Sed 3542193326Sed return TemplateName(QTN); 3543193326Sed} 3544193326Sed 3545198092Srdivacky/// \brief Retrieve the template name that represents a qualified 3546198092Srdivacky/// template name such as \c std::vector. 3547198092SrdivackyTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 3548198092Srdivacky bool TemplateKeyword, 3549198092Srdivacky OverloadedFunctionDecl *Template) { 3550198092Srdivacky llvm::FoldingSetNodeID ID; 3551198092Srdivacky QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); 3552198092Srdivacky 3553198092Srdivacky void *InsertPos = 0; 3554198092Srdivacky QualifiedTemplateName *QTN = 3555198092Srdivacky QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 3556198092Srdivacky if (!QTN) { 3557198092Srdivacky QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); 3558198092Srdivacky QualifiedTemplateNames.InsertNode(QTN, InsertPos); 3559198092Srdivacky } 3560198092Srdivacky 3561198092Srdivacky return TemplateName(QTN); 3562198092Srdivacky} 3563198092Srdivacky 3564193326Sed/// \brief Retrieve the template name that represents a dependent 3565193326Sed/// template name such as \c MetaFun::template apply. 3566198092SrdivackyTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, 3567193326Sed const IdentifierInfo *Name) { 3568198092Srdivacky assert((!NNS || NNS->isDependent()) && 3569198092Srdivacky "Nested name specifier must be dependent"); 3570193326Sed 3571193326Sed llvm::FoldingSetNodeID ID; 3572193326Sed DependentTemplateName::Profile(ID, NNS, Name); 3573193326Sed 3574193326Sed void *InsertPos = 0; 3575193326Sed DependentTemplateName *QTN = 3576193326Sed DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 3577193326Sed 3578193326Sed if (QTN) 3579193326Sed return TemplateName(QTN); 3580193326Sed 3581193326Sed NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 3582193326Sed if (CanonNNS == NNS) { 3583193326Sed QTN = new (*this,4) DependentTemplateName(NNS, Name); 3584193326Sed } else { 3585193326Sed TemplateName Canon = getDependentTemplateName(CanonNNS, Name); 3586193326Sed QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); 3587193326Sed } 3588193326Sed 3589193326Sed DependentTemplateNames.InsertNode(QTN, InsertPos); 3590193326Sed return TemplateName(QTN); 3591193326Sed} 3592193326Sed 3593193326Sed/// getFromTargetType - Given one of the integer types provided by 3594193326Sed/// TargetInfo, produce the corresponding type. The unsigned @p Type 3595193326Sed/// is actually a value of type @c TargetInfo::IntType. 3596193326SedQualType ASTContext::getFromTargetType(unsigned Type) const { 3597193326Sed switch (Type) { 3598198092Srdivacky case TargetInfo::NoInt: return QualType(); 3599193326Sed case TargetInfo::SignedShort: return ShortTy; 3600193326Sed case TargetInfo::UnsignedShort: return UnsignedShortTy; 3601193326Sed case TargetInfo::SignedInt: return IntTy; 3602193326Sed case TargetInfo::UnsignedInt: return UnsignedIntTy; 3603193326Sed case TargetInfo::SignedLong: return LongTy; 3604193326Sed case TargetInfo::UnsignedLong: return UnsignedLongTy; 3605193326Sed case TargetInfo::SignedLongLong: return LongLongTy; 3606193326Sed case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; 3607193326Sed } 3608193326Sed 3609193326Sed assert(false && "Unhandled TargetInfo::IntType value"); 3610193326Sed return QualType(); 3611193326Sed} 3612193326Sed 3613193326Sed//===----------------------------------------------------------------------===// 3614193326Sed// Type Predicates. 3615193326Sed//===----------------------------------------------------------------------===// 3616193326Sed 3617193326Sed/// isObjCNSObjectType - Return true if this is an NSObject object using 3618193326Sed/// NSObject attribute on a c-style pointer type. 3619193326Sed/// FIXME - Make it work directly on types. 3620198092Srdivacky/// FIXME: Move to Type. 3621193326Sed/// 3622193326Sedbool ASTContext::isObjCNSObjectType(QualType Ty) const { 3623193326Sed if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { 3624193326Sed if (TypedefDecl *TD = TDT->getDecl()) 3625195341Sed if (TD->getAttr<ObjCNSObjectAttr>()) 3626193326Sed return true; 3627193326Sed } 3628193326Sed return false; 3629193326Sed} 3630193326Sed 3631193326Sed/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 3632193326Sed/// garbage collection attribute. 3633193326Sed/// 3634198092SrdivackyQualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { 3635198092Srdivacky Qualifiers::GC GCAttrs = Qualifiers::GCNone; 3636193326Sed if (getLangOptions().ObjC1 && 3637193326Sed getLangOptions().getGCMode() != LangOptions::NonGC) { 3638193326Sed GCAttrs = Ty.getObjCGCAttr(); 3639193326Sed // Default behavious under objective-c's gc is for objective-c pointers 3640198092Srdivacky // (or pointers to them) be treated as though they were declared 3641193326Sed // as __strong. 3642198092Srdivacky if (GCAttrs == Qualifiers::GCNone) { 3643198092Srdivacky if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) 3644198092Srdivacky GCAttrs = Qualifiers::Strong; 3645193326Sed else if (Ty->isPointerType()) 3646198092Srdivacky return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); 3647193326Sed } 3648193326Sed // Non-pointers have none gc'able attribute regardless of the attribute 3649193326Sed // set on them. 3650198092Srdivacky else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) 3651198092Srdivacky return Qualifiers::GCNone; 3652193326Sed } 3653193326Sed return GCAttrs; 3654193326Sed} 3655193326Sed 3656193326Sed//===----------------------------------------------------------------------===// 3657193326Sed// Type Compatibility Testing 3658193326Sed//===----------------------------------------------------------------------===// 3659193326Sed 3660198092Srdivacky/// areCompatVectorTypes - Return true if the two specified vector types are 3661193326Sed/// compatible. 3662193326Sedstatic bool areCompatVectorTypes(const VectorType *LHS, 3663193326Sed const VectorType *RHS) { 3664198398Srdivacky assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); 3665193326Sed return LHS->getElementType() == RHS->getElementType() && 3666193326Sed LHS->getNumElements() == RHS->getNumElements(); 3667193326Sed} 3668193326Sed 3669198092Srdivacky//===----------------------------------------------------------------------===// 3670198092Srdivacky// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. 3671198092Srdivacky//===----------------------------------------------------------------------===// 3672198092Srdivacky 3673198092Srdivacky/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the 3674198092Srdivacky/// inheritance hierarchy of 'rProto'. 3675198092Srdivackybool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 3676198092Srdivacky ObjCProtocolDecl *rProto) { 3677198092Srdivacky if (lProto == rProto) 3678198092Srdivacky return true; 3679198092Srdivacky for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), 3680198092Srdivacky E = rProto->protocol_end(); PI != E; ++PI) 3681198092Srdivacky if (ProtocolCompatibleWithProtocol(lProto, *PI)) 3682198092Srdivacky return true; 3683198092Srdivacky return false; 3684198092Srdivacky} 3685198092Srdivacky 3686198092Srdivacky/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> 3687198092Srdivacky/// return true if lhs's protocols conform to rhs's protocol; false 3688198092Srdivacky/// otherwise. 3689198092Srdivackybool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { 3690198092Srdivacky if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) 3691198092Srdivacky return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); 3692198092Srdivacky return false; 3693198092Srdivacky} 3694198092Srdivacky 3695198092Srdivacky/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an 3696198092Srdivacky/// ObjCQualifiedIDType. 3697198092Srdivackybool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, 3698198092Srdivacky bool compare) { 3699198092Srdivacky // Allow id<P..> and an 'id' or void* type in all cases. 3700198092Srdivacky if (lhs->isVoidPointerType() || 3701198092Srdivacky lhs->isObjCIdType() || lhs->isObjCClassType()) 3702198092Srdivacky return true; 3703198092Srdivacky else if (rhs->isVoidPointerType() || 3704198092Srdivacky rhs->isObjCIdType() || rhs->isObjCClassType()) 3705198092Srdivacky return true; 3706198092Srdivacky 3707198092Srdivacky if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { 3708198092Srdivacky const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); 3709198092Srdivacky 3710198092Srdivacky if (!rhsOPT) return false; 3711198092Srdivacky 3712198092Srdivacky if (rhsOPT->qual_empty()) { 3713198092Srdivacky // If the RHS is a unqualified interface pointer "NSString*", 3714198092Srdivacky // make sure we check the class hierarchy. 3715198092Srdivacky if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { 3716198092Srdivacky for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 3717198092Srdivacky E = lhsQID->qual_end(); I != E; ++I) { 3718198092Srdivacky // when comparing an id<P> on lhs with a static type on rhs, 3719198092Srdivacky // see if static class implements all of id's protocols, directly or 3720198092Srdivacky // through its super class and categories. 3721198092Srdivacky if (!rhsID->ClassImplementsProtocol(*I, true)) 3722198092Srdivacky return false; 3723198092Srdivacky } 3724198092Srdivacky } 3725198092Srdivacky // If there are no qualifiers and no interface, we have an 'id'. 3726198092Srdivacky return true; 3727198092Srdivacky } 3728198092Srdivacky // Both the right and left sides have qualifiers. 3729198092Srdivacky for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 3730198092Srdivacky E = lhsQID->qual_end(); I != E; ++I) { 3731198092Srdivacky ObjCProtocolDecl *lhsProto = *I; 3732198092Srdivacky bool match = false; 3733198092Srdivacky 3734198092Srdivacky // when comparing an id<P> on lhs with a static type on rhs, 3735198092Srdivacky // see if static class implements all of id's protocols, directly or 3736198092Srdivacky // through its super class and categories. 3737198092Srdivacky for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), 3738198092Srdivacky E = rhsOPT->qual_end(); J != E; ++J) { 3739198092Srdivacky ObjCProtocolDecl *rhsProto = *J; 3740198092Srdivacky if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || 3741198092Srdivacky (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { 3742198092Srdivacky match = true; 3743198092Srdivacky break; 3744198092Srdivacky } 3745198092Srdivacky } 3746198092Srdivacky // If the RHS is a qualified interface pointer "NSString<P>*", 3747198092Srdivacky // make sure we check the class hierarchy. 3748198092Srdivacky if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { 3749198092Srdivacky for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 3750198092Srdivacky E = lhsQID->qual_end(); I != E; ++I) { 3751198092Srdivacky // when comparing an id<P> on lhs with a static type on rhs, 3752198092Srdivacky // see if static class implements all of id's protocols, directly or 3753198092Srdivacky // through its super class and categories. 3754198092Srdivacky if (rhsID->ClassImplementsProtocol(*I, true)) { 3755198092Srdivacky match = true; 3756198092Srdivacky break; 3757198092Srdivacky } 3758198092Srdivacky } 3759198092Srdivacky } 3760198092Srdivacky if (!match) 3761198092Srdivacky return false; 3762198092Srdivacky } 3763198092Srdivacky 3764198092Srdivacky return true; 3765198092Srdivacky } 3766198092Srdivacky 3767198092Srdivacky const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); 3768198092Srdivacky assert(rhsQID && "One of the LHS/RHS should be id<x>"); 3769198092Srdivacky 3770198092Srdivacky if (const ObjCObjectPointerType *lhsOPT = 3771198092Srdivacky lhs->getAsObjCInterfacePointerType()) { 3772198092Srdivacky if (lhsOPT->qual_empty()) { 3773198092Srdivacky bool match = false; 3774198092Srdivacky if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { 3775198092Srdivacky for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), 3776198092Srdivacky E = rhsQID->qual_end(); I != E; ++I) { 3777198092Srdivacky // when comparing an id<P> on lhs with a static type on rhs, 3778198092Srdivacky // see if static class implements all of id's protocols, directly or 3779198092Srdivacky // through its super class and categories. 3780198092Srdivacky if (lhsID->ClassImplementsProtocol(*I, true)) { 3781198092Srdivacky match = true; 3782198092Srdivacky break; 3783198092Srdivacky } 3784198092Srdivacky } 3785198092Srdivacky if (!match) 3786198092Srdivacky return false; 3787198092Srdivacky } 3788198092Srdivacky return true; 3789198092Srdivacky } 3790198092Srdivacky // Both the right and left sides have qualifiers. 3791198092Srdivacky for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), 3792198092Srdivacky E = lhsOPT->qual_end(); I != E; ++I) { 3793198092Srdivacky ObjCProtocolDecl *lhsProto = *I; 3794198092Srdivacky bool match = false; 3795198092Srdivacky 3796198092Srdivacky // when comparing an id<P> on lhs with a static type on rhs, 3797198092Srdivacky // see if static class implements all of id's protocols, directly or 3798198092Srdivacky // through its super class and categories. 3799198092Srdivacky for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), 3800198092Srdivacky E = rhsQID->qual_end(); J != E; ++J) { 3801198092Srdivacky ObjCProtocolDecl *rhsProto = *J; 3802198092Srdivacky if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || 3803198092Srdivacky (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { 3804198092Srdivacky match = true; 3805198092Srdivacky break; 3806198092Srdivacky } 3807198092Srdivacky } 3808198092Srdivacky if (!match) 3809198092Srdivacky return false; 3810198092Srdivacky } 3811198092Srdivacky return true; 3812198092Srdivacky } 3813198092Srdivacky return false; 3814198092Srdivacky} 3815198092Srdivacky 3816193326Sed/// canAssignObjCInterfaces - Return true if the two interface types are 3817193326Sed/// compatible for assignment from RHS to LHS. This handles validation of any 3818193326Sed/// protocol qualifiers on the LHS or RHS. 3819193326Sed/// 3820198092Srdivackybool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 3821198092Srdivacky const ObjCObjectPointerType *RHSOPT) { 3822198092Srdivacky // If either type represents the built-in 'id' or 'Class' types, return true. 3823198092Srdivacky if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) 3824198092Srdivacky return true; 3825198092Srdivacky 3826198092Srdivacky if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) 3827198092Srdivacky return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), 3828198092Srdivacky QualType(RHSOPT,0), 3829198092Srdivacky false); 3830198092Srdivacky 3831198092Srdivacky const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); 3832198092Srdivacky const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); 3833198092Srdivacky if (LHS && RHS) // We have 2 user-defined types. 3834198092Srdivacky return canAssignObjCInterfaces(LHS, RHS); 3835198092Srdivacky 3836198092Srdivacky return false; 3837198092Srdivacky} 3838198092Srdivacky 3839193326Sedbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 3840193326Sed const ObjCInterfaceType *RHS) { 3841193326Sed // Verify that the base decls are compatible: the RHS must be a subclass of 3842193326Sed // the LHS. 3843193326Sed if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) 3844193326Sed return false; 3845198092Srdivacky 3846193326Sed // RHS must have a superset of the protocols in the LHS. If the LHS is not 3847193326Sed // protocol qualified at all, then we are good. 3848198092Srdivacky if (LHS->getNumProtocols() == 0) 3849193326Sed return true; 3850198092Srdivacky 3851193326Sed // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it 3852193326Sed // isn't a superset. 3853198092Srdivacky if (RHS->getNumProtocols() == 0) 3854193326Sed return true; // FIXME: should return false! 3855198092Srdivacky 3856198092Srdivacky for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), 3857198092Srdivacky LHSPE = LHS->qual_end(); 3858193326Sed LHSPI != LHSPE; LHSPI++) { 3859193326Sed bool RHSImplementsProtocol = false; 3860193326Sed 3861193326Sed // If the RHS doesn't implement the protocol on the left, the types 3862193326Sed // are incompatible. 3863198092Srdivacky for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), 3864198092Srdivacky RHSPE = RHS->qual_end(); 3865198092Srdivacky RHSPI != RHSPE; RHSPI++) { 3866198092Srdivacky if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { 3867193326Sed RHSImplementsProtocol = true; 3868198092Srdivacky break; 3869198092Srdivacky } 3870193326Sed } 3871193326Sed // FIXME: For better diagnostics, consider passing back the protocol name. 3872193326Sed if (!RHSImplementsProtocol) 3873193326Sed return false; 3874193326Sed } 3875193326Sed // The RHS implements all protocols listed on the LHS. 3876193326Sed return true; 3877193326Sed} 3878193326Sed 3879193326Sedbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { 3880193326Sed // get the "pointed to" types 3881198092Srdivacky const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); 3882198092Srdivacky const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); 3883198092Srdivacky 3884198092Srdivacky if (!LHSOPT || !RHSOPT) 3885193326Sed return false; 3886198092Srdivacky 3887198092Srdivacky return canAssignObjCInterfaces(LHSOPT, RHSOPT) || 3888198092Srdivacky canAssignObjCInterfaces(RHSOPT, LHSOPT); 3889193326Sed} 3890193326Sed 3891198092Srdivacky/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, 3892193326Sed/// both shall have the identically qualified version of a compatible type. 3893198092Srdivacky/// C99 6.2.7p1: Two types have compatible types if their types are the 3894193326Sed/// same. See 6.7.[2,3,5] for additional rules. 3895193326Sedbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { 3896193326Sed return !mergeTypes(LHS, RHS).isNull(); 3897193326Sed} 3898193326Sed 3899193326SedQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { 3900198092Srdivacky const FunctionType *lbase = lhs->getAs<FunctionType>(); 3901198092Srdivacky const FunctionType *rbase = rhs->getAs<FunctionType>(); 3902193326Sed const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); 3903193326Sed const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); 3904193326Sed bool allLTypes = true; 3905193326Sed bool allRTypes = true; 3906193326Sed 3907193326Sed // Check return type 3908193326Sed QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); 3909193326Sed if (retType.isNull()) return QualType(); 3910193326Sed if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) 3911193326Sed allLTypes = false; 3912193326Sed if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) 3913193326Sed allRTypes = false; 3914198092Srdivacky // FIXME: double check this 3915198092Srdivacky bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); 3916198092Srdivacky if (NoReturn != lbase->getNoReturnAttr()) 3917198092Srdivacky allLTypes = false; 3918198092Srdivacky if (NoReturn != rbase->getNoReturnAttr()) 3919198092Srdivacky allRTypes = false; 3920193326Sed 3921193326Sed if (lproto && rproto) { // two C99 style function prototypes 3922193326Sed assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && 3923193326Sed "C++ shouldn't be here"); 3924193326Sed unsigned lproto_nargs = lproto->getNumArgs(); 3925193326Sed unsigned rproto_nargs = rproto->getNumArgs(); 3926193326Sed 3927193326Sed // Compatible functions must have the same number of arguments 3928193326Sed if (lproto_nargs != rproto_nargs) 3929193326Sed return QualType(); 3930193326Sed 3931193326Sed // Variadic and non-variadic functions aren't compatible 3932193326Sed if (lproto->isVariadic() != rproto->isVariadic()) 3933193326Sed return QualType(); 3934193326Sed 3935193326Sed if (lproto->getTypeQuals() != rproto->getTypeQuals()) 3936193326Sed return QualType(); 3937193326Sed 3938193326Sed // Check argument compatibility 3939193326Sed llvm::SmallVector<QualType, 10> types; 3940193326Sed for (unsigned i = 0; i < lproto_nargs; i++) { 3941193326Sed QualType largtype = lproto->getArgType(i).getUnqualifiedType(); 3942193326Sed QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); 3943193326Sed QualType argtype = mergeTypes(largtype, rargtype); 3944193326Sed if (argtype.isNull()) return QualType(); 3945193326Sed types.push_back(argtype); 3946193326Sed if (getCanonicalType(argtype) != getCanonicalType(largtype)) 3947193326Sed allLTypes = false; 3948193326Sed if (getCanonicalType(argtype) != getCanonicalType(rargtype)) 3949193326Sed allRTypes = false; 3950193326Sed } 3951193326Sed if (allLTypes) return lhs; 3952193326Sed if (allRTypes) return rhs; 3953193326Sed return getFunctionType(retType, types.begin(), types.size(), 3954198092Srdivacky lproto->isVariadic(), lproto->getTypeQuals(), 3955198092Srdivacky NoReturn); 3956193326Sed } 3957193326Sed 3958193326Sed if (lproto) allRTypes = false; 3959193326Sed if (rproto) allLTypes = false; 3960193326Sed 3961193326Sed const FunctionProtoType *proto = lproto ? lproto : rproto; 3962193326Sed if (proto) { 3963193326Sed assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); 3964193326Sed if (proto->isVariadic()) return QualType(); 3965193326Sed // Check that the types are compatible with the types that 3966193326Sed // would result from default argument promotions (C99 6.7.5.3p15). 3967193326Sed // The only types actually affected are promotable integer 3968193326Sed // types and floats, which would be passed as a different 3969193326Sed // type depending on whether the prototype is visible. 3970193326Sed unsigned proto_nargs = proto->getNumArgs(); 3971193326Sed for (unsigned i = 0; i < proto_nargs; ++i) { 3972193326Sed QualType argTy = proto->getArgType(i); 3973193326Sed if (argTy->isPromotableIntegerType() || 3974193326Sed getCanonicalType(argTy).getUnqualifiedType() == FloatTy) 3975193326Sed return QualType(); 3976193326Sed } 3977193326Sed 3978193326Sed if (allLTypes) return lhs; 3979193326Sed if (allRTypes) return rhs; 3980193326Sed return getFunctionType(retType, proto->arg_type_begin(), 3981198092Srdivacky proto->getNumArgs(), proto->isVariadic(), 3982198092Srdivacky proto->getTypeQuals(), NoReturn); 3983193326Sed } 3984193326Sed 3985193326Sed if (allLTypes) return lhs; 3986193326Sed if (allRTypes) return rhs; 3987198092Srdivacky return getFunctionNoProtoType(retType, NoReturn); 3988193326Sed} 3989193326Sed 3990193326SedQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { 3991193326Sed // C++ [expr]: If an expression initially has the type "reference to T", the 3992193326Sed // type is adjusted to "T" prior to any further analysis, the expression 3993193326Sed // designates the object or function denoted by the reference, and the 3994193326Sed // expression is an lvalue unless the reference is an rvalue reference and 3995193326Sed // the expression is a function call (possibly inside parentheses). 3996193326Sed // FIXME: C++ shouldn't be going through here! The rules are different 3997193326Sed // enough that they should be handled separately. 3998193326Sed // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* 3999193326Sed // shouldn't be going through here! 4000198092Srdivacky if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) 4001193326Sed LHS = RT->getPointeeType(); 4002198092Srdivacky if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) 4003193326Sed RHS = RT->getPointeeType(); 4004193326Sed 4005193326Sed QualType LHSCan = getCanonicalType(LHS), 4006193326Sed RHSCan = getCanonicalType(RHS); 4007193326Sed 4008193326Sed // If two types are identical, they are compatible. 4009193326Sed if (LHSCan == RHSCan) 4010193326Sed return LHS; 4011193326Sed 4012198092Srdivacky // If the qualifiers are different, the types aren't compatible... mostly. 4013198092Srdivacky Qualifiers LQuals = LHSCan.getQualifiers(); 4014198092Srdivacky Qualifiers RQuals = RHSCan.getQualifiers(); 4015198092Srdivacky if (LQuals != RQuals) { 4016198092Srdivacky // If any of these qualifiers are different, we have a type 4017198092Srdivacky // mismatch. 4018198092Srdivacky if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || 4019198092Srdivacky LQuals.getAddressSpace() != RQuals.getAddressSpace()) 4020198092Srdivacky return QualType(); 4021198092Srdivacky 4022198092Srdivacky // Exactly one GC qualifier difference is allowed: __strong is 4023198092Srdivacky // okay if the other type has no GC qualifier but is an Objective 4024198092Srdivacky // C object pointer (i.e. implicitly strong by default). We fix 4025198092Srdivacky // this by pretending that the unqualified type was actually 4026198092Srdivacky // qualified __strong. 4027198092Srdivacky Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); 4028198092Srdivacky Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); 4029198092Srdivacky assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); 4030198092Srdivacky 4031198092Srdivacky if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) 4032198092Srdivacky return QualType(); 4033198092Srdivacky 4034198092Srdivacky if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { 4035198092Srdivacky return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); 4036198092Srdivacky } 4037198092Srdivacky if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { 4038198092Srdivacky return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); 4039198092Srdivacky } 4040193326Sed return QualType(); 4041198092Srdivacky } 4042193326Sed 4043198092Srdivacky // Okay, qualifiers are equal. 4044198092Srdivacky 4045193326Sed Type::TypeClass LHSClass = LHSCan->getTypeClass(); 4046193326Sed Type::TypeClass RHSClass = RHSCan->getTypeClass(); 4047193326Sed 4048193326Sed // We want to consider the two function types to be the same for these 4049193326Sed // comparisons, just force one to the other. 4050193326Sed if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; 4051193326Sed if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; 4052193326Sed 4053193326Sed // Same as above for arrays 4054193326Sed if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) 4055193326Sed LHSClass = Type::ConstantArray; 4056193326Sed if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) 4057193326Sed RHSClass = Type::ConstantArray; 4058198092Srdivacky 4059193326Sed // Canonicalize ExtVector -> Vector. 4060193326Sed if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; 4061193326Sed if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; 4062193326Sed 4063193326Sed // If the canonical type classes don't match. 4064193326Sed if (LHSClass != RHSClass) { 4065193326Sed // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, 4066198092Srdivacky // a signed integer type, or an unsigned integer type. 4067198092Srdivacky if (const EnumType* ETy = LHS->getAs<EnumType>()) { 4068193326Sed if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) 4069193326Sed return RHS; 4070193326Sed } 4071198092Srdivacky if (const EnumType* ETy = RHS->getAs<EnumType>()) { 4072193326Sed if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) 4073193326Sed return LHS; 4074193326Sed } 4075193326Sed 4076193326Sed return QualType(); 4077193326Sed } 4078193326Sed 4079193326Sed // The canonical type classes match. 4080193326Sed switch (LHSClass) { 4081193326Sed#define TYPE(Class, Base) 4082193326Sed#define ABSTRACT_TYPE(Class, Base) 4083193326Sed#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 4084193326Sed#define DEPENDENT_TYPE(Class, Base) case Type::Class: 4085193326Sed#include "clang/AST/TypeNodes.def" 4086193326Sed assert(false && "Non-canonical and dependent types shouldn't get here"); 4087193326Sed return QualType(); 4088193326Sed 4089193326Sed case Type::LValueReference: 4090193326Sed case Type::RValueReference: 4091193326Sed case Type::MemberPointer: 4092193326Sed assert(false && "C++ should never be in mergeTypes"); 4093193326Sed return QualType(); 4094193326Sed 4095193326Sed case Type::IncompleteArray: 4096193326Sed case Type::VariableArray: 4097193326Sed case Type::FunctionProto: 4098193326Sed case Type::ExtVector: 4099193326Sed assert(false && "Types are eliminated above"); 4100193326Sed return QualType(); 4101193326Sed 4102193326Sed case Type::Pointer: 4103193326Sed { 4104193326Sed // Merge two pointer types, while trying to preserve typedef info 4105198092Srdivacky QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); 4106198092Srdivacky QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); 4107193326Sed QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 4108193326Sed if (ResultType.isNull()) return QualType(); 4109193326Sed if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 4110193326Sed return LHS; 4111193326Sed if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 4112193326Sed return RHS; 4113193326Sed return getPointerType(ResultType); 4114193326Sed } 4115193326Sed case Type::BlockPointer: 4116193326Sed { 4117193326Sed // Merge two block pointer types, while trying to preserve typedef info 4118198092Srdivacky QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); 4119198092Srdivacky QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); 4120193326Sed QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 4121193326Sed if (ResultType.isNull()) return QualType(); 4122193326Sed if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 4123193326Sed return LHS; 4124193326Sed if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 4125193326Sed return RHS; 4126193326Sed return getBlockPointerType(ResultType); 4127193326Sed } 4128193326Sed case Type::ConstantArray: 4129193326Sed { 4130193326Sed const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); 4131193326Sed const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); 4132193326Sed if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) 4133193326Sed return QualType(); 4134193326Sed 4135193326Sed QualType LHSElem = getAsArrayType(LHS)->getElementType(); 4136193326Sed QualType RHSElem = getAsArrayType(RHS)->getElementType(); 4137193326Sed QualType ResultType = mergeTypes(LHSElem, RHSElem); 4138193326Sed if (ResultType.isNull()) return QualType(); 4139193326Sed if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 4140193326Sed return LHS; 4141193326Sed if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 4142193326Sed return RHS; 4143193326Sed if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), 4144193326Sed ArrayType::ArraySizeModifier(), 0); 4145193326Sed if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), 4146193326Sed ArrayType::ArraySizeModifier(), 0); 4147193326Sed const VariableArrayType* LVAT = getAsVariableArrayType(LHS); 4148193326Sed const VariableArrayType* RVAT = getAsVariableArrayType(RHS); 4149193326Sed if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 4150193326Sed return LHS; 4151193326Sed if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 4152193326Sed return RHS; 4153193326Sed if (LVAT) { 4154193326Sed // FIXME: This isn't correct! But tricky to implement because 4155193326Sed // the array's size has to be the size of LHS, but the type 4156193326Sed // has to be different. 4157193326Sed return LHS; 4158193326Sed } 4159193326Sed if (RVAT) { 4160193326Sed // FIXME: This isn't correct! But tricky to implement because 4161193326Sed // the array's size has to be the size of RHS, but the type 4162193326Sed // has to be different. 4163193326Sed return RHS; 4164193326Sed } 4165193326Sed if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; 4166193326Sed if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; 4167198092Srdivacky return getIncompleteArrayType(ResultType, 4168198092Srdivacky ArrayType::ArraySizeModifier(), 0); 4169193326Sed } 4170193326Sed case Type::FunctionNoProto: 4171193326Sed return mergeFunctionTypes(LHS, RHS); 4172193326Sed case Type::Record: 4173193326Sed case Type::Enum: 4174193326Sed return QualType(); 4175193326Sed case Type::Builtin: 4176193326Sed // Only exactly equal builtin types are compatible, which is tested above. 4177193326Sed return QualType(); 4178193326Sed case Type::Complex: 4179193326Sed // Distinct complex types are incompatible. 4180193326Sed return QualType(); 4181193326Sed case Type::Vector: 4182193326Sed // FIXME: The merged type should be an ExtVector! 4183198092Srdivacky if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>())) 4184193326Sed return LHS; 4185193326Sed return QualType(); 4186193326Sed case Type::ObjCInterface: { 4187193326Sed // Check if the interfaces are assignment compatible. 4188193326Sed // FIXME: This should be type compatibility, e.g. whether 4189193326Sed // "LHS x; RHS x;" at global scope is legal. 4190198092Srdivacky const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); 4191198092Srdivacky const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); 4192193326Sed if (LHSIface && RHSIface && 4193193326Sed canAssignObjCInterfaces(LHSIface, RHSIface)) 4194193326Sed return LHS; 4195193326Sed 4196193326Sed return QualType(); 4197193326Sed } 4198198092Srdivacky case Type::ObjCObjectPointer: { 4199198092Srdivacky if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), 4200198092Srdivacky RHS->getAs<ObjCObjectPointerType>())) 4201198092Srdivacky return LHS; 4202198092Srdivacky 4203193326Sed return QualType(); 4204198092Srdivacky } 4205193326Sed case Type::FixedWidthInt: 4206193326Sed // Distinct fixed-width integers are not compatible. 4207193326Sed return QualType(); 4208193326Sed case Type::TemplateSpecialization: 4209193326Sed assert(false && "Dependent types have no size"); 4210193326Sed break; 4211193326Sed } 4212193326Sed 4213193326Sed return QualType(); 4214193326Sed} 4215193326Sed 4216193326Sed//===----------------------------------------------------------------------===// 4217193326Sed// Integer Predicates 4218193326Sed//===----------------------------------------------------------------------===// 4219193326Sed 4220193326Sedunsigned ASTContext::getIntWidth(QualType T) { 4221193326Sed if (T == BoolTy) 4222193326Sed return 1; 4223198398Srdivacky if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) { 4224193326Sed return FWIT->getWidth(); 4225193326Sed } 4226193326Sed // For builtin types, just use the standard type sizing method 4227193326Sed return (unsigned)getTypeSize(T); 4228193326Sed} 4229193326Sed 4230193326SedQualType ASTContext::getCorrespondingUnsignedType(QualType T) { 4231193326Sed assert(T->isSignedIntegerType() && "Unexpected type"); 4232198398Srdivacky 4233198398Srdivacky // Turn <4 x signed int> -> <4 x unsigned int> 4234198398Srdivacky if (const VectorType *VTy = T->getAs<VectorType>()) 4235198398Srdivacky return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), 4236198398Srdivacky VTy->getNumElements()); 4237198398Srdivacky 4238198398Srdivacky // For enums, we return the unsigned version of the base type. 4239198398Srdivacky if (const EnumType *ETy = T->getAs<EnumType>()) 4240193326Sed T = ETy->getDecl()->getIntegerType(); 4241198398Srdivacky 4242198398Srdivacky const BuiltinType *BTy = T->getAs<BuiltinType>(); 4243198398Srdivacky assert(BTy && "Unexpected signed integer type"); 4244193326Sed switch (BTy->getKind()) { 4245193326Sed case BuiltinType::Char_S: 4246193326Sed case BuiltinType::SChar: 4247193326Sed return UnsignedCharTy; 4248193326Sed case BuiltinType::Short: 4249193326Sed return UnsignedShortTy; 4250193326Sed case BuiltinType::Int: 4251193326Sed return UnsignedIntTy; 4252193326Sed case BuiltinType::Long: 4253193326Sed return UnsignedLongTy; 4254193326Sed case BuiltinType::LongLong: 4255193326Sed return UnsignedLongLongTy; 4256193326Sed case BuiltinType::Int128: 4257193326Sed return UnsignedInt128Ty; 4258193326Sed default: 4259193326Sed assert(0 && "Unexpected signed integer type"); 4260193326Sed return QualType(); 4261193326Sed } 4262193326Sed} 4263193326Sed 4264193326SedExternalASTSource::~ExternalASTSource() { } 4265193326Sed 4266193326Sedvoid ExternalASTSource::PrintStats() { } 4267194179Sed 4268194179Sed 4269194179Sed//===----------------------------------------------------------------------===// 4270194179Sed// Builtin Type Computation 4271194179Sed//===----------------------------------------------------------------------===// 4272194179Sed 4273194179Sed/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the 4274194179Sed/// pointer over the consumed characters. This returns the resultant type. 4275198092Srdivackystatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, 4276194179Sed ASTContext::GetBuiltinTypeError &Error, 4277194179Sed bool AllowTypeModifiers = true) { 4278194179Sed // Modifiers. 4279194179Sed int HowLong = 0; 4280194179Sed bool Signed = false, Unsigned = false; 4281198092Srdivacky 4282194179Sed // Read the modifiers first. 4283194179Sed bool Done = false; 4284194179Sed while (!Done) { 4285194179Sed switch (*Str++) { 4286198092Srdivacky default: Done = true; --Str; break; 4287194179Sed case 'S': 4288194179Sed assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); 4289194179Sed assert(!Signed && "Can't use 'S' modifier multiple times!"); 4290194179Sed Signed = true; 4291194179Sed break; 4292194179Sed case 'U': 4293194179Sed assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); 4294194179Sed assert(!Unsigned && "Can't use 'S' modifier multiple times!"); 4295194179Sed Unsigned = true; 4296194179Sed break; 4297194179Sed case 'L': 4298194179Sed assert(HowLong <= 2 && "Can't have LLLL modifier"); 4299194179Sed ++HowLong; 4300194179Sed break; 4301194179Sed } 4302194179Sed } 4303194179Sed 4304194179Sed QualType Type; 4305198092Srdivacky 4306194179Sed // Read the base type. 4307194179Sed switch (*Str++) { 4308194179Sed default: assert(0 && "Unknown builtin type letter!"); 4309194179Sed case 'v': 4310194179Sed assert(HowLong == 0 && !Signed && !Unsigned && 4311194179Sed "Bad modifiers used with 'v'!"); 4312194179Sed Type = Context.VoidTy; 4313194179Sed break; 4314194179Sed case 'f': 4315194179Sed assert(HowLong == 0 && !Signed && !Unsigned && 4316194179Sed "Bad modifiers used with 'f'!"); 4317194179Sed Type = Context.FloatTy; 4318194179Sed break; 4319194179Sed case 'd': 4320194179Sed assert(HowLong < 2 && !Signed && !Unsigned && 4321194179Sed "Bad modifiers used with 'd'!"); 4322194179Sed if (HowLong) 4323194179Sed Type = Context.LongDoubleTy; 4324194179Sed else 4325194179Sed Type = Context.DoubleTy; 4326194179Sed break; 4327194179Sed case 's': 4328194179Sed assert(HowLong == 0 && "Bad modifiers used with 's'!"); 4329194179Sed if (Unsigned) 4330194179Sed Type = Context.UnsignedShortTy; 4331194179Sed else 4332194179Sed Type = Context.ShortTy; 4333194179Sed break; 4334194179Sed case 'i': 4335194179Sed if (HowLong == 3) 4336194179Sed Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; 4337194179Sed else if (HowLong == 2) 4338194179Sed Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; 4339194179Sed else if (HowLong == 1) 4340194179Sed Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; 4341194179Sed else 4342194179Sed Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; 4343194179Sed break; 4344194179Sed case 'c': 4345194179Sed assert(HowLong == 0 && "Bad modifiers used with 'c'!"); 4346194179Sed if (Signed) 4347194179Sed Type = Context.SignedCharTy; 4348194179Sed else if (Unsigned) 4349194179Sed Type = Context.UnsignedCharTy; 4350194179Sed else 4351194179Sed Type = Context.CharTy; 4352194179Sed break; 4353194179Sed case 'b': // boolean 4354194179Sed assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); 4355194179Sed Type = Context.BoolTy; 4356194179Sed break; 4357194179Sed case 'z': // size_t. 4358194179Sed assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); 4359194179Sed Type = Context.getSizeType(); 4360194179Sed break; 4361194179Sed case 'F': 4362194179Sed Type = Context.getCFConstantStringType(); 4363194179Sed break; 4364194179Sed case 'a': 4365194179Sed Type = Context.getBuiltinVaListType(); 4366194179Sed assert(!Type.isNull() && "builtin va list type not initialized!"); 4367194179Sed break; 4368194179Sed case 'A': 4369194179Sed // This is a "reference" to a va_list; however, what exactly 4370194179Sed // this means depends on how va_list is defined. There are two 4371194179Sed // different kinds of va_list: ones passed by value, and ones 4372194179Sed // passed by reference. An example of a by-value va_list is 4373194179Sed // x86, where va_list is a char*. An example of by-ref va_list 4374194179Sed // is x86-64, where va_list is a __va_list_tag[1]. For x86, 4375194179Sed // we want this argument to be a char*&; for x86-64, we want 4376194179Sed // it to be a __va_list_tag*. 4377194179Sed Type = Context.getBuiltinVaListType(); 4378194179Sed assert(!Type.isNull() && "builtin va list type not initialized!"); 4379194179Sed if (Type->isArrayType()) { 4380194179Sed Type = Context.getArrayDecayedType(Type); 4381194179Sed } else { 4382194179Sed Type = Context.getLValueReferenceType(Type); 4383194179Sed } 4384194179Sed break; 4385194179Sed case 'V': { 4386194179Sed char *End; 4387194179Sed unsigned NumElements = strtoul(Str, &End, 10); 4388194179Sed assert(End != Str && "Missing vector size"); 4389198092Srdivacky 4390194179Sed Str = End; 4391198092Srdivacky 4392194179Sed QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); 4393194179Sed Type = Context.getVectorType(ElementType, NumElements); 4394194179Sed break; 4395194179Sed } 4396198092Srdivacky case 'X': { 4397198092Srdivacky QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); 4398198092Srdivacky Type = Context.getComplexType(ElementType); 4399198092Srdivacky break; 4400198092Srdivacky } 4401198092Srdivacky case 'P': 4402198092Srdivacky Type = Context.getFILEType(); 4403198092Srdivacky if (Type.isNull()) { 4404198092Srdivacky Error = ASTContext::GE_Missing_stdio; 4405198092Srdivacky return QualType(); 4406194179Sed } 4407198092Srdivacky break; 4408198092Srdivacky case 'J': 4409198092Srdivacky if (Signed) 4410198092Srdivacky Type = Context.getsigjmp_bufType(); 4411198092Srdivacky else 4412198092Srdivacky Type = Context.getjmp_bufType(); 4413198092Srdivacky 4414198092Srdivacky if (Type.isNull()) { 4415198092Srdivacky Error = ASTContext::GE_Missing_setjmp; 4416194179Sed return QualType(); 4417194179Sed } 4418198092Srdivacky break; 4419194179Sed } 4420198092Srdivacky 4421194179Sed if (!AllowTypeModifiers) 4422194179Sed return Type; 4423198092Srdivacky 4424194179Sed Done = false; 4425194179Sed while (!Done) { 4426194179Sed switch (*Str++) { 4427194179Sed default: Done = true; --Str; break; 4428194179Sed case '*': 4429194179Sed Type = Context.getPointerType(Type); 4430194179Sed break; 4431194179Sed case '&': 4432194179Sed Type = Context.getLValueReferenceType(Type); 4433194179Sed break; 4434194179Sed // FIXME: There's no way to have a built-in with an rvalue ref arg. 4435194179Sed case 'C': 4436198092Srdivacky Type = Type.withConst(); 4437194179Sed break; 4438194179Sed } 4439194179Sed } 4440198092Srdivacky 4441194179Sed return Type; 4442194179Sed} 4443194179Sed 4444194179Sed/// GetBuiltinType - Return the type for the specified builtin. 4445194179SedQualType ASTContext::GetBuiltinType(unsigned id, 4446194179Sed GetBuiltinTypeError &Error) { 4447194179Sed const char *TypeStr = BuiltinInfo.GetTypeString(id); 4448198092Srdivacky 4449194179Sed llvm::SmallVector<QualType, 8> ArgTypes; 4450198092Srdivacky 4451194179Sed Error = GE_None; 4452194179Sed QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); 4453194179Sed if (Error != GE_None) 4454194179Sed return QualType(); 4455194179Sed while (TypeStr[0] && TypeStr[0] != '.') { 4456194179Sed QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); 4457194179Sed if (Error != GE_None) 4458194179Sed return QualType(); 4459194179Sed 4460194179Sed // Do array -> pointer decay. The builtin should use the decayed type. 4461194179Sed if (Ty->isArrayType()) 4462194179Sed Ty = getArrayDecayedType(Ty); 4463198092Srdivacky 4464194179Sed ArgTypes.push_back(Ty); 4465194179Sed } 4466194179Sed 4467194179Sed assert((TypeStr[0] != '.' || TypeStr[1] == 0) && 4468194179Sed "'.' should only occur at end of builtin type list!"); 4469194179Sed 4470194179Sed // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". 4471194179Sed if (ArgTypes.size() == 0 && TypeStr[0] == '.') 4472194179Sed return getFunctionNoProtoType(ResType); 4473194179Sed return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), 4474194179Sed TypeStr[0] == '.', 0); 4475194179Sed} 4476198092Srdivacky 4477198092SrdivackyQualType 4478198092SrdivackyASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { 4479198092Srdivacky // Perform the usual unary conversions. We do this early so that 4480198092Srdivacky // integral promotions to "int" can allow us to exit early, in the 4481198092Srdivacky // lhs == rhs check. Also, for conversion purposes, we ignore any 4482198092Srdivacky // qualifiers. For example, "const float" and "float" are 4483198092Srdivacky // equivalent. 4484198092Srdivacky if (lhs->isPromotableIntegerType()) 4485198092Srdivacky lhs = getPromotedIntegerType(lhs); 4486198092Srdivacky else 4487198092Srdivacky lhs = lhs.getUnqualifiedType(); 4488198092Srdivacky if (rhs->isPromotableIntegerType()) 4489198092Srdivacky rhs = getPromotedIntegerType(rhs); 4490198092Srdivacky else 4491198092Srdivacky rhs = rhs.getUnqualifiedType(); 4492198092Srdivacky 4493198092Srdivacky // If both types are identical, no conversion is needed. 4494198092Srdivacky if (lhs == rhs) 4495198092Srdivacky return lhs; 4496198092Srdivacky 4497198092Srdivacky // If either side is a non-arithmetic type (e.g. a pointer), we are done. 4498198092Srdivacky // The caller can deal with this (e.g. pointer + int). 4499198092Srdivacky if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) 4500198092Srdivacky return lhs; 4501198092Srdivacky 4502198092Srdivacky // At this point, we have two different arithmetic types. 4503198092Srdivacky 4504198092Srdivacky // Handle complex types first (C99 6.3.1.8p1). 4505198092Srdivacky if (lhs->isComplexType() || rhs->isComplexType()) { 4506198092Srdivacky // if we have an integer operand, the result is the complex type. 4507198092Srdivacky if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { 4508198092Srdivacky // convert the rhs to the lhs complex type. 4509198092Srdivacky return lhs; 4510198092Srdivacky } 4511198092Srdivacky if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { 4512198092Srdivacky // convert the lhs to the rhs complex type. 4513198092Srdivacky return rhs; 4514198092Srdivacky } 4515198092Srdivacky // This handles complex/complex, complex/float, or float/complex. 4516198092Srdivacky // When both operands are complex, the shorter operand is converted to the 4517198092Srdivacky // type of the longer, and that is the type of the result. This corresponds 4518198092Srdivacky // to what is done when combining two real floating-point operands. 4519198092Srdivacky // The fun begins when size promotion occur across type domains. 4520198092Srdivacky // From H&S 6.3.4: When one operand is complex and the other is a real 4521198092Srdivacky // floating-point type, the less precise type is converted, within it's 4522198092Srdivacky // real or complex domain, to the precision of the other type. For example, 4523198092Srdivacky // when combining a "long double" with a "double _Complex", the 4524198092Srdivacky // "double _Complex" is promoted to "long double _Complex". 4525198092Srdivacky int result = getFloatingTypeOrder(lhs, rhs); 4526198092Srdivacky 4527198092Srdivacky if (result > 0) { // The left side is bigger, convert rhs. 4528198092Srdivacky rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); 4529198092Srdivacky } else if (result < 0) { // The right side is bigger, convert lhs. 4530198092Srdivacky lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); 4531198092Srdivacky } 4532198092Srdivacky // At this point, lhs and rhs have the same rank/size. Now, make sure the 4533198092Srdivacky // domains match. This is a requirement for our implementation, C99 4534198092Srdivacky // does not require this promotion. 4535198092Srdivacky if (lhs != rhs) { // Domains don't match, we have complex/float mix. 4536198092Srdivacky if (lhs->isRealFloatingType()) { // handle "double, _Complex double". 4537198092Srdivacky return rhs; 4538198092Srdivacky } else { // handle "_Complex double, double". 4539198092Srdivacky return lhs; 4540198092Srdivacky } 4541198092Srdivacky } 4542198092Srdivacky return lhs; // The domain/size match exactly. 4543198092Srdivacky } 4544198092Srdivacky // Now handle "real" floating types (i.e. float, double, long double). 4545198092Srdivacky if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { 4546198092Srdivacky // if we have an integer operand, the result is the real floating type. 4547198092Srdivacky if (rhs->isIntegerType()) { 4548198092Srdivacky // convert rhs to the lhs floating point type. 4549198092Srdivacky return lhs; 4550198092Srdivacky } 4551198092Srdivacky if (rhs->isComplexIntegerType()) { 4552198092Srdivacky // convert rhs to the complex floating point type. 4553198092Srdivacky return getComplexType(lhs); 4554198092Srdivacky } 4555198092Srdivacky if (lhs->isIntegerType()) { 4556198092Srdivacky // convert lhs to the rhs floating point type. 4557198092Srdivacky return rhs; 4558198092Srdivacky } 4559198092Srdivacky if (lhs->isComplexIntegerType()) { 4560198092Srdivacky // convert lhs to the complex floating point type. 4561198092Srdivacky return getComplexType(rhs); 4562198092Srdivacky } 4563198092Srdivacky // We have two real floating types, float/complex combos were handled above. 4564198092Srdivacky // Convert the smaller operand to the bigger result. 4565198092Srdivacky int result = getFloatingTypeOrder(lhs, rhs); 4566198092Srdivacky if (result > 0) // convert the rhs 4567198092Srdivacky return lhs; 4568198092Srdivacky assert(result < 0 && "illegal float comparison"); 4569198092Srdivacky return rhs; // convert the lhs 4570198092Srdivacky } 4571198092Srdivacky if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { 4572198092Srdivacky // Handle GCC complex int extension. 4573198092Srdivacky const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); 4574198092Srdivacky const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); 4575198092Srdivacky 4576198092Srdivacky if (lhsComplexInt && rhsComplexInt) { 4577198092Srdivacky if (getIntegerTypeOrder(lhsComplexInt->getElementType(), 4578198092Srdivacky rhsComplexInt->getElementType()) >= 0) 4579198092Srdivacky return lhs; // convert the rhs 4580198092Srdivacky return rhs; 4581198092Srdivacky } else if (lhsComplexInt && rhs->isIntegerType()) { 4582198092Srdivacky // convert the rhs to the lhs complex type. 4583198092Srdivacky return lhs; 4584198092Srdivacky } else if (rhsComplexInt && lhs->isIntegerType()) { 4585198092Srdivacky // convert the lhs to the rhs complex type. 4586198092Srdivacky return rhs; 4587198092Srdivacky } 4588198092Srdivacky } 4589198092Srdivacky // Finally, we have two differing integer types. 4590198092Srdivacky // The rules for this case are in C99 6.3.1.8 4591198092Srdivacky int compare = getIntegerTypeOrder(lhs, rhs); 4592198092Srdivacky bool lhsSigned = lhs->isSignedIntegerType(), 4593198092Srdivacky rhsSigned = rhs->isSignedIntegerType(); 4594198092Srdivacky QualType destType; 4595198092Srdivacky if (lhsSigned == rhsSigned) { 4596198092Srdivacky // Same signedness; use the higher-ranked type 4597198092Srdivacky destType = compare >= 0 ? lhs : rhs; 4598198092Srdivacky } else if (compare != (lhsSigned ? 1 : -1)) { 4599198092Srdivacky // The unsigned type has greater than or equal rank to the 4600198092Srdivacky // signed type, so use the unsigned type 4601198092Srdivacky destType = lhsSigned ? rhs : lhs; 4602198092Srdivacky } else if (getIntWidth(lhs) != getIntWidth(rhs)) { 4603198092Srdivacky // The two types are different widths; if we are here, that 4604198092Srdivacky // means the signed type is larger than the unsigned type, so 4605198092Srdivacky // use the signed type. 4606198092Srdivacky destType = lhsSigned ? lhs : rhs; 4607198092Srdivacky } else { 4608198092Srdivacky // The signed type is higher-ranked than the unsigned type, 4609198092Srdivacky // but isn't actually any bigger (like unsigned int and long 4610198092Srdivacky // on most 32-bit systems). Use the unsigned type corresponding 4611198092Srdivacky // to the signed type. 4612198092Srdivacky destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); 4613198092Srdivacky } 4614198092Srdivacky return destType; 4615198092Srdivacky} 4616