ASTContext.cpp revision d8b285fee4471f393da8ee30f552ceacdc362afa
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the ASTContext interface. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 1549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h" 16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h" 17aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h" 18b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h" 19e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h" 202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h" 2119cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h" 221b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h" 23a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h" 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 2585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h" 266fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h" 27557c5b1717bc8919b1b40cf2064b51491ec53a44Chris Lattner#include "llvm/Support/MemoryBuffer.h" 2829445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson#include "RecordLayoutBuilder.h" 2929445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank { 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer FloatRank, DoubleRank, LongDoubleRank 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3661710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, 3761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner TargetInfo &t, 38e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar IdentifierTable &idents, SelectorTable &sels, 391b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner Builtin::Context &builtins, 401b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner bool FreeMem, unsigned size_reserve) : 41ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), 42782fa308a765aeac2acb39c4e697c937ec21185bMike Stump ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), 43782fa308a765aeac2acb39c4e697c937ec21185bMike Stump sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts), 442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LoadedExternalComments(false), FreeMemory(FreeMem), Target(t), 452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Idents(idents), Selectors(sels), 46e4f2142d00fa5fdb580c4e2413da91882d955381Chris Lattner BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) { 470f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall ObjCIdRedefinitionType = QualType(); 480f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall ObjCClassRedefinitionType = QualType(); 49e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar if (size_reserve > 0) Types.reserve(size_reserve); 50e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar TUDecl = TranslationUnitDecl::Create(*this); 5114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff InitBuiltinTypes(); 52e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar} 53e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar 545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() { 555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Deallocate all the types. 565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer while (!Types.empty()) { 574b05b1dee6cc65ae61d93dab7edff72710f24589Ted Kremenek Types.back()->Destroy(*this); 585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.pop_back(); 595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 60b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman 61b74668edbc119880eb0a7e563432314432cb775dNuno Lopes { 62b74668edbc119880eb0a7e563432314432cb775dNuno Lopes llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator 63b74668edbc119880eb0a7e563432314432cb775dNuno Lopes I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); 64b74668edbc119880eb0a7e563432314432cb775dNuno Lopes while (I != E) { 65b74668edbc119880eb0a7e563432314432cb775dNuno Lopes ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); 66b74668edbc119880eb0a7e563432314432cb775dNuno Lopes delete R; 67b74668edbc119880eb0a7e563432314432cb775dNuno Lopes } 68b74668edbc119880eb0a7e563432314432cb775dNuno Lopes } 69b74668edbc119880eb0a7e563432314432cb775dNuno Lopes 70b74668edbc119880eb0a7e563432314432cb775dNuno Lopes { 71b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator 72b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar I = ObjCLayouts.begin(), E = ObjCLayouts.end(); 73b74668edbc119880eb0a7e563432314432cb775dNuno Lopes while (I != E) { 74b74668edbc119880eb0a7e563432314432cb775dNuno Lopes ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); 75b74668edbc119880eb0a7e563432314432cb775dNuno Lopes delete R; 76b74668edbc119880eb0a7e563432314432cb775dNuno Lopes } 77b74668edbc119880eb0a7e563432314432cb775dNuno Lopes } 78b74668edbc119880eb0a7e563432314432cb775dNuno Lopes 79ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor // Destroy nested-name-specifiers. 801ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor for (llvm::FoldingSet<NestedNameSpecifier>::iterator 811ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor NNS = NestedNameSpecifiers.begin(), 821ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor NNSEnd = NestedNameSpecifiers.end(); 83e7dcd782df0e1cc3a63e429a866098e69db39153Douglas Gregor NNS != NNSEnd; 841ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor /* Increment in loop */) 851ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor (*NNS++).Destroy(*this); 86ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor 87ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor if (GlobalNestedNameSpecifier) 88ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor GlobalNestedNameSpecifier->Destroy(*this); 89ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor 90b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman TUDecl->Destroy(*this); 915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid 942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { 952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor ExternalSource.reset(Source.take()); 962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor} 972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor 985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const { 995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer fprintf(stderr, "*** AST Context Stats:\n"); 1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer fprintf(stderr, " %d types total.\n", (int)Types.size()); 1017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 102dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor unsigned counts[] = { 103dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent) 0, 104dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent) 105dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def" 106dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor 0 // Extra 107dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor }; 108c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor 1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0, e = Types.size(); i != e; ++i) { 1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Type *T = Types[i]; 111dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor counts[(unsigned)T->getTypeClass()]++; 1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 114dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor unsigned Idx = 0; 115dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor unsigned TotalBytes = 0; 116dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent) \ 117dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor if (counts[Idx]) \ 118dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ 119dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor TotalBytes += counts[Idx] * sizeof(Name##Type); \ 120dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor ++Idx; 121dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent) 122dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def" 123dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor 124dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); 1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor 1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor if (ExternalSource.get()) { 1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor fprintf(stderr, "\n"); 1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor ExternalSource->PrintStats(); 1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor } 1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { 134f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr()); 1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() { 1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer assert(VoidTy.isNull() && "Context reinitialized?"); 1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.2.5p19. 1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(VoidTy, BuiltinType::Void); 1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.2.5p2. 1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(BoolTy, BuiltinType::Bool); 1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.2.5p3. 14615b91764d08e886391c865c4a444d7b51141c284Eli Friedman if (LangOpts.CharIsSigned) 1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(CharTy, BuiltinType::Char_S); 1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer else 1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(CharTy, BuiltinType::Char_U); 1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.2.5p4. 1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(SignedCharTy, BuiltinType::SChar); 1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(ShortTy, BuiltinType::Short); 1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(IntTy, BuiltinType::Int); 1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(LongTy, BuiltinType::Long); 1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(LongLongTy, BuiltinType::LongLong); 1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.2.5p6. 1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); 1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); 1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); 1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); 1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); 1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.2.5p10. 1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(FloatTy, BuiltinType::Float); 1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(DoubleTy, BuiltinType::Double); 1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); 16864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis 1692df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner // GNU extension, 128-bit integers. 1702df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner InitBuiltinType(Int128Ty, BuiltinType::Int128); 1712df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); 1722df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner 1733a2503227c3db04a3619735127483263c1075ef7Chris Lattner if (LangOpts.CPlusPlus) // C++ 3.9.1p5 1743a2503227c3db04a3619735127483263c1075ef7Chris Lattner InitBuiltinType(WCharTy, BuiltinType::WChar); 1753a2503227c3db04a3619735127483263c1075ef7Chris Lattner else // C99 1763a2503227c3db04a3619735127483263c1075ef7Chris Lattner WCharTy = getFromTargetType(Target.getWCharType()); 17764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis 178f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ 179f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith InitBuiltinType(Char16Ty, BuiltinType::Char16); 180f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith else // C99 181f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Char16Ty = getFromTargetType(Target.getChar16Type()); 182f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith 183f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ 184f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith InitBuiltinType(Char32Ty, BuiltinType::Char32); 185f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith else // C99 186f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Char32Ty = getFromTargetType(Target.getChar32Type()); 187f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith 1888e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor // Placeholder type for functions. 189898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor InitBuiltinType(OverloadTy, BuiltinType::Overload); 190898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 191898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // Placeholder type for type-dependent expressions whose type is 192898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // completely unknown. No code should ever check a type against 193898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // DependentTy and users should never see it; however, it is here to 194898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // help diagnose failures to properly check for type-dependent 195898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor // expressions. 196898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor InitBuiltinType(DependentTy, BuiltinType::Dependent); 1978e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor 198e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson // Placeholder type for C++0x auto declarations whose real type has 199e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson // not yet been deduced. 200e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); 201e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson 2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // C99 6.2.5p11. 2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer FloatComplexTy = getComplexType(FloatTy); 2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer DoubleComplexTy = getComplexType(DoubleTy); 2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LongDoubleComplexTy = getComplexType(LongDoubleTy); 2068e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor 2077e219e47de26346885d667131977bd9ca2d7662aSteve Naroff BuiltinVaListType = QualType(); 20814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 209de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). 210de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff ObjCIdTypedefType = QualType(); 211de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff ObjCClassTypedefType = QualType(); 212de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff 213de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // Builtin types for 'id' and 'Class'. 214de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); 215de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); 21614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 217a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCConstantStringType = QualType(); 21833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 21933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // void * type 22033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian VoidPtrTy = getPointerType(VoidTy); 2216e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl 2226e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl // nullptr type (C++0x 2.14.7) 2236e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); 2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorVarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) { 2277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Var->isStaticDataMember() && "Not a static data member"); 2287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos 2297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor = InstantiatedFromStaticDataMember.find(Var); 2307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (Pos == InstantiatedFromStaticDataMember.end()) 2317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return 0; 2327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 2337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return Pos->second; 2347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor} 2357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 2367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid 2377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) { 2387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Inst->isStaticDataMember() && "Not a static data member"); 2397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(Tmpl->isStaticDataMember() && "Not a static data member"); 2407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor assert(!InstantiatedFromStaticDataMember[Inst] && 2417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor "Already noted what static data member was instantiated from"); 2427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor InstantiatedFromStaticDataMember[Inst] = Tmpl; 2437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor} 2447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor 2450d8df780aef1acda5962347a32591efc629b6748Anders CarlssonUnresolvedUsingDecl * 2460d8df780aef1acda5962347a32591efc629b6748Anders CarlssonASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) { 2470d8df780aef1acda5962347a32591efc629b6748Anders Carlsson llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos 2480d8df780aef1acda5962347a32591efc629b6748Anders Carlsson = InstantiatedFromUnresolvedUsingDecl.find(UUD); 2490d8df780aef1acda5962347a32591efc629b6748Anders Carlsson if (Pos == InstantiatedFromUnresolvedUsingDecl.end()) 2500d8df780aef1acda5962347a32591efc629b6748Anders Carlsson return 0; 2510d8df780aef1acda5962347a32591efc629b6748Anders Carlsson 2520d8df780aef1acda5962347a32591efc629b6748Anders Carlsson return Pos->second; 2530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson} 2540d8df780aef1acda5962347a32591efc629b6748Anders Carlsson 2550d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid 2560d8df780aef1acda5962347a32591efc629b6748Anders CarlssonASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD, 2570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson UnresolvedUsingDecl *UUD) { 2580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson assert(!InstantiatedFromUnresolvedUsingDecl[UD] && 2590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson "Already noted what using decl what instantiated from"); 2600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson InstantiatedFromUnresolvedUsingDecl[UD] = UUD; 2610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson} 2620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson 263d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { 264d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos 265d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson = InstantiatedFromUnnamedFieldDecl.find(Field); 266d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson if (Pos == InstantiatedFromUnnamedFieldDecl.end()) 267d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson return 0; 268d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson 269d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson return Pos->second; 270d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson} 271d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson 272d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, 273d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson FieldDecl *Tmpl) { 274d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); 275d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); 276d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson assert(!InstantiatedFromUnnamedFieldDecl[Inst] && 277d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson "Already noted what unnamed field was instantiated from"); 278d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson 279d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; 280d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson} 281d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson 2822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace { 2832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor class BeforeInTranslationUnit 2842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor : std::binary_function<SourceRange, SourceRange, bool> { 2852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceManager *SourceMgr; 2862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor public: 2882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } 2892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor bool operator()(SourceRange X, SourceRange Y) { 2912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); 2922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 2932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor }; 2942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor} 2952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Determine whether the given comment is a Doxygen-style comment. 2972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// 2982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Start the start of the comment text. 2992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// 3002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param End the end of the comment text. 3012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// 3022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Member whether we want to check whether this is a member comment 3032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// (which requires a < after the Doxygen-comment delimiter). Otherwise, 3042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// we only return true when we find a non-member comment. 3052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorstatic bool 3062e22253e03e175144aeb9d13350a12fd83f858beDouglas GregorisDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, 3072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor bool Member = false) { 3082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char *BufferStart 3092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; 3102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); 3112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); 3122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (End - Start < 4) 3142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return false; 3152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor assert(Start[0] == '/' && "Not a comment?"); 3172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) 3182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return false; 3192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) 3202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return false; 3212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return (Start[3] == '<') == Member; 3232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor} 3242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Retrieve the comment associated with the given declaration, if 3262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// it has one. 3272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorconst char *ASTContext::getCommentForDecl(const Decl *D) { 3282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!D) 3292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Check whether we have cached a comment string for this declaration 3322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // already. 3332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor llvm::DenseMap<const Decl *, std::string>::iterator Pos 3342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = DeclComments.find(D); 3352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Pos != DeclComments.end()) 3362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return Pos->second.c_str(); 3372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If we have an external AST source and have not yet loaded comments from 3392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // that source, do so now. 3402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (ExternalSource && !LoadedExternalComments) { 3412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::vector<SourceRange> LoadedComments; 3422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ExternalSource->ReadComments(LoadedComments); 3432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!LoadedComments.empty()) 3452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Comments.insert(Comments.begin(), LoadedComments.begin(), 3462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LoadedComments.end()); 3472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LoadedExternalComments = true; 3492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 3502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If there are no comments anywhere, we won't find anything. 3522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Comments.empty()) 3532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the declaration doesn't map directly to a location in a file, we 3562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // can't find the comment. 3572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceLocation DeclStartLoc = D->getLocStart(); 3582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) 3592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Find the comment that occurs just before this declaration. 3622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::vector<SourceRange>::iterator LastComment 3632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = std::lower_bound(Comments.begin(), Comments.end(), 3642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceRange(DeclStartLoc), 3652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor BeforeInTranslationUnit(&SourceMgr)); 3662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Decompose the location for the start of the declaration and find the 3682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // beginning of the file buffer. 3692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> DeclStartDecomp 3702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(DeclStartLoc); 3712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char *FileBufferStart 3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getBufferData(DeclStartDecomp.first).first; 3732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // First check whether we have a comment for a member. 3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (LastComment != Comments.end() && 3762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && 3772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor isDoxygenComment(SourceMgr, *LastComment, true)) { 3782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> LastCommentEndDecomp 3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(LastComment->getEnd()); 3802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (DeclStartDecomp.first == LastCommentEndDecomp.first && 3812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) 3822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor == SourceMgr.getLineNumber(LastCommentEndDecomp.first, 3832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LastCommentEndDecomp.second)) { 3842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // The Doxygen member comment comes after the declaration starts and 3852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // is on the same line and in the same file as the declaration. This 3862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // is the comment we want. 3872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::string &Result = DeclComments[D]; 3882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Result.append(FileBufferStart + 3892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceMgr.getFileOffset(LastComment->getBegin()), 3902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor FileBufferStart + LastCommentEndDecomp.second + 1); 3912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return Result.c_str(); 3922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 3932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 3942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (LastComment == Comments.begin()) 3962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor --LastComment; 3982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Decompose the end of the comment. 4002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> LastCommentEndDecomp 4012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(LastComment->getEnd()); 4022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the comment and the declaration aren't in the same file, then they 4042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // aren't related. 4052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (DeclStartDecomp.first != LastCommentEndDecomp.first) 4062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 4072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Check that we actually have a Doxygen comment. 4092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!isDoxygenComment(SourceMgr, *LastComment)) 4102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 4112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Compute the starting line for the declaration and for the end of the 4132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // comment (this is expensive). 4142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned DeclStartLine 4152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); 4162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned CommentEndLine 4172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getLineNumber(LastCommentEndDecomp.first, 4182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LastCommentEndDecomp.second); 4192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the comment does not end on the line prior to the declaration, then 4212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // the comment is not associated with the declaration at all. 4222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (CommentEndLine + 1 != DeclStartLine) 4232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 4242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // We have a comment, but there may be more comments on the previous lines. 4262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Keep looking so long as the comments are still Doxygen comments and are 4272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // still adjacent. 4282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned ExpectedLine 4292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; 4302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::vector<SourceRange>::iterator FirstComment = LastComment; 4312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor while (FirstComment != Comments.begin()) { 4322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Look at the previous comment 4332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor --FirstComment; 4342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> Decomp 4352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); 4362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If this previous comment is in a different file, we're done. 4382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Decomp.first != DeclStartDecomp.first) { 4392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor break; 4412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If this comment is not a Doxygen comment, we're done. 4442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!isDoxygenComment(SourceMgr, *FirstComment)) { 4452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor break; 4472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the line number is not what we expected, we're done. 4502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); 4512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Line != ExpectedLine) { 4522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor break; 4542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Set the next expected line number. 4572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ExpectedLine 4582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; 4592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // The iterator range [FirstComment, LastComment] contains all of the 4622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // BCPL comments that, together, are associated with this declaration. 4632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Form a single comment block string for this declaration that concatenates 4642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // all of these comments. 4652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::string &Result = DeclComments[D]; 4662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor while (FirstComment != LastComment) { 4672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> DecompStart 4682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); 4692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> DecompEnd 4702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); 4712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Result.append(FileBufferStart + DecompStart.second, 4722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor FileBufferStart + DecompEnd.second + 1); 4732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Append the last comment line. 4772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Result.append(FileBufferStart + 4782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceMgr.getFileOffset(LastComment->getBegin()), 4792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor FileBufferStart + LastCommentEndDecomp.second + 1); 4802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return Result.c_str(); 4812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor} 4822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 483464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===// 484464175bba1318bef7905122e9fda20cff926df78Chris Lattner// Type Sizing and Analysis 485464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===// 486a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 487b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 488b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type. 489b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { 490b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const BuiltinType *BT = T->getAsBuiltinType(); 491b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner assert(BT && "Not a floating point type!"); 492b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner switch (BT->getKind()) { 493b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner default: assert(0 && "Not a floating point type!"); 494b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner case BuiltinType::Float: return Target.getFloatFormat(); 495b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner case BuiltinType::Double: return Target.getDoubleFormat(); 496b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); 497b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 498b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner} 499b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 500af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// getDeclAlign - Return a conservative estimate of the alignment of the 501af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl. Note that bitfields do not have a valid alignment, so 502af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them. 503b7d0844c887a72064b624dc6df12cbe1441f69d0Daniel Dunbarunsigned ASTContext::getDeclAlignInBytes(const Decl *D) { 504dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman unsigned Align = Target.getCharWidth(); 505dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 50640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) 507dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman Align = std::max(Align, AA->getAlignment()); 508dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 509af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { 510af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner QualType T = VD->getType(); 5116217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType* RT = T->getAs<ReferenceType>()) { 5124cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson unsigned AS = RT->getPointeeType().getAddressSpace(); 513f0930235ce58a91aa3b840bece9052f44d630536Anders Carlsson Align = Target.getPointerAlign(AS); 5144cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson } else if (!T->isIncompleteType() && !T->isFunctionType()) { 5154cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson // Incomplete or function types default to 1. 516dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) 517dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman T = cast<ArrayType>(T)->getElementType(); 518dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 519dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); 520dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman } 521af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner } 522dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 523dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman return Align / Target.getCharWidth(); 524af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner} 525b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 526a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits. This method 527a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types. 528d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned> 5291d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) { 5305e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump uint64_t Width=0; 5315e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump unsigned Align=8; 532a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner switch (T->getTypeClass()) { 53372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base) 53472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 53518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base) 53672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class: 53772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def" 53818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor assert(false && "Should not see dependent types"); 53972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor break; 54072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 5415d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner case Type::FunctionNoProto: 5425d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner case Type::FunctionProto: 54318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // GCC extension: alignof(function) = 32 bits 54418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = 0; 54518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = 32; 54618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor break; 54718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 54872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::IncompleteArray: 549fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::VariableArray: 55018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = 0; 55118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); 55218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor break; 55318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 5547e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor case Type::ConstantArrayWithExpr: 5557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor case Type::ConstantArrayWithoutExpr: 556fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::ConstantArray: { 5571d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const ConstantArrayType *CAT = cast<ConstantArrayType>(T); 558030d8846c7e520330007087e949f621989876e3aChris Lattner 55998be4943e8dc4f3905629a7102668960873cf863Chris Lattner std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); 5609e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = EltInfo.first*CAT->getSize().getZExtValue(); 561030d8846c7e520330007087e949f621989876e3aChris Lattner Align = EltInfo.second; 562030d8846c7e520330007087e949f621989876e3aChris Lattner break; 5635c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb } 564213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman case Type::ExtVector: 565030d8846c7e520330007087e949f621989876e3aChris Lattner case Type::Vector: { 566030d8846c7e520330007087e949f621989876e3aChris Lattner std::pair<uint64_t, unsigned> EltInfo = 56798be4943e8dc4f3905629a7102668960873cf863Chris Lattner getTypeInfo(cast<VectorType>(T)->getElementType()); 5689e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); 5694bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman Align = Width; 5706fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman // If the alignment is not a power of 2, round up to the next power of 2. 5716fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman // This happens for non-power-of-2 length vectors. 5726fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman // FIXME: this should probably be a target property. 5736fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman Align = 1 << llvm::Log2_32_Ceil(Align); 574030d8846c7e520330007087e949f621989876e3aChris Lattner break; 575030d8846c7e520330007087e949f621989876e3aChris Lattner } 5765d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner 5779e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner case Type::Builtin: 578a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner switch (cast<BuiltinType>(T)->getKind()) { 579692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner default: assert(0 && "Unknown builtin type!"); 580d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::Void: 58118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // GCC extension: alignof(void) = 8 bits. 58218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = 0; 58318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = 8; 58418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor break; 58518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 5866f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Bool: 5879e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getBoolWidth(); 5889e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getBoolAlign(); 5896f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 590692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::Char_S: 591692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::Char_U: 592692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::UChar: 5936f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::SChar: 5949e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getCharWidth(); 5959e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getCharAlign(); 5966f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 59764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis case BuiltinType::WChar: 59864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis Width = Target.getWCharWidth(); 59964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis Align = Target.getWCharAlign(); 60064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis break; 601f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char16: 602f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Width = Target.getChar16Width(); 603f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Align = Target.getChar16Align(); 604f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 605f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char32: 606f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Width = Target.getChar32Width(); 607f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Align = Target.getChar32Align(); 608f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 609692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::UShort: 6106f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Short: 6119e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getShortWidth(); 6129e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getShortAlign(); 6136f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 614692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::UInt: 6156f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Int: 6169e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getIntWidth(); 6179e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getIntAlign(); 6186f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 619692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::ULong: 6206f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Long: 6219e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getLongWidth(); 6229e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getLongAlign(); 6236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 624692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::ULongLong: 6256f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::LongLong: 6269e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getLongLongWidth(); 6279e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getLongLongAlign(); 6286f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 629ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner case BuiltinType::Int128: 630ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner case BuiltinType::UInt128: 631ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner Width = 128; 632ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner Align = 128; // int128_t is 128-bit aligned on all targets. 633ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner break; 6346f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Float: 6359e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getFloatWidth(); 6369e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getFloatAlign(); 6376f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 6386f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Double: 6395426bf6456a5aeac416a9150de157904d101c819Chris Lattner Width = Target.getDoubleWidth(); 6405426bf6456a5aeac416a9150de157904d101c819Chris Lattner Align = Target.getDoubleAlign(); 6416f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 6426f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::LongDouble: 6439e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getLongDoubleWidth(); 6449e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getLongDoubleAlign(); 6456f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 6466e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl case BuiltinType::NullPtr: 6476e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) 6486e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl Align = Target.getPointerAlign(0); // == sizeof(void*) 6491590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl break; 650a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner } 651bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner break; 652f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman case Type::FixedWidthInt: 653f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // FIXME: This isn't precisely correct; the width/alignment should depend 654f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // on the available types for the target 655f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman Width = cast<FixedWidthIntType>(T)->getWidth(); 656736166b38235cf6d0ffb67638960d95fb2afcbd6Chris Lattner Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); 657f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman Align = Width; 658f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman break; 659f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian case Type::ExtQual: 66098be4943e8dc4f3905629a7102668960873cf863Chris Lattner // FIXME: Pointers into different addr spaces could have different sizes and 66198be4943e8dc4f3905629a7102668960873cf863Chris Lattner // alignment requirements: getPointerInfo should take an AddrSpace. 662f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0)); 663d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff case Type::ObjCObjectPointer: 6645426bf6456a5aeac416a9150de157904d101c819Chris Lattner Width = Target.getPointerWidth(0); 665f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner Align = Target.getPointerAlign(0); 6666f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 667485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff case Type::BlockPointer: { 668485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); 669485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff Width = Target.getPointerWidth(AS); 670485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff Align = Target.getPointerAlign(AS); 671485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff break; 672485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff } 673f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner case Type::Pointer: { 674f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); 6755426bf6456a5aeac416a9150de157904d101c819Chris Lattner Width = Target.getPointerWidth(AS); 676f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner Align = Target.getPointerAlign(AS); 677f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner break; 678f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner } 6797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::LValueReference: 6807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::RValueReference: 6817ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner // "When applied to a reference or a reference type, the result is the size 6825d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner // of the referenced type." C++98 5.3.3p2: expr.sizeof. 6836f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner // FIXME: This is wrong for struct layout: a reference in a struct has 6846f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner // pointer size. 685bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); 686f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl case Type::MemberPointer: { 6871cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson // FIXME: This is ABI dependent. We use the Itanium C++ ABI. 6881cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers 6891cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson // If we ever want to support other ABIs this needs to be abstracted. 6901cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson 691f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); 6921cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson std::pair<uint64_t, unsigned> PtrDiffInfo = 6931cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson getTypeInfo(getPointerDiffType()); 6941cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson Width = PtrDiffInfo.first; 695f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl if (Pointee->isFunctionType()) 696f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl Width *= 2; 6971cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson Align = PtrDiffInfo.second; 6981cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson break; 699f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl } 7005d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner case Type::Complex: { 7015d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner // Complex types have the same alignment as their elements, but twice the 7025d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner // size. 7035d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner std::pair<uint64_t, unsigned> EltInfo = 70498be4943e8dc4f3905629a7102668960873cf863Chris Lattner getTypeInfo(cast<ComplexType>(T)->getElementType()); 7059e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = EltInfo.first*2; 7065d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner Align = EltInfo.second; 7075d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner break; 7085d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner } 70944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel case Type::ObjCInterface: { 7101d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); 71144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); 71244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel Width = Layout.getSize(); 71344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel Align = Layout.getAlignment(); 71444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel break; 71544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel } 71672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Record: 71772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Enum: { 7181d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const TagType *TT = cast<TagType>(T); 7191d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar 7201d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar if (TT->getDecl()->isInvalidDecl()) { 7218389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner Width = 1; 7228389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner Align = 1; 7238389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner break; 7248389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner } 7258389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner 7261d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar if (const EnumType *ET = dyn_cast<EnumType>(TT)) 7277176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner return getTypeInfo(ET->getDecl()->getIntegerType()); 7287176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner 7291d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const RecordType *RT = cast<RecordType>(TT); 7307176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); 7317176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner Width = Layout.getSize(); 7327176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner Align = Layout.getAlignment(); 733dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner break; 734a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner } 7357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 73618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::Typedef: { 73718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); 73840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { 73918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = Aligned->getAlignment(); 74018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); 74118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor } else 74218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); 7437532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor break; 7447176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner } 74518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 74618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::TypeOfExpr: 74718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() 74818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor .getTypePtr()); 74918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 75018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::TypeOf: 75118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); 75218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 753395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson case Type::Decltype: 754395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() 755395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson .getTypePtr()); 756395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson 75718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::QualifiedName: 75818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); 75918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 76018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::TemplateSpecialization: 76118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor assert(getCanonicalType(T) != T && 76218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor "Cannot request the size of a dependent type"); 76318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // FIXME: this is likely to be wrong once we support template 76418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // aliases, since a template alias could refer to a typedef that 76518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // has an __aligned__ attribute on it. 76618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(getCanonicalType(T)); 76718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor } 768d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner 769464175bba1318bef7905122e9fda20cff926df78Chris Lattner assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); 7709e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner return std::make_pair(Width, Align); 771a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner} 772a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 77334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified 77434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits. This can be different than the ABI 77534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign 77634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type. 77734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) { 77834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner unsigned ABIAlign = getTypeAlign(T); 7791eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman 7801eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman // Double and long long should be naturally aligned if possible. 7811eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman if (const ComplexType* CT = T->getAsComplexType()) 7821eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman T = CT->getElementType().getTypePtr(); 7831eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman if (T->isSpecificBuiltinType(BuiltinType::Double) || 7841eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman T->isSpecificBuiltinType(BuiltinType::LongLong)) 7851eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman return std::max(ABIAlign, (unsigned)getTypeSize(T)); 7861eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman 78734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner return ABIAlign; 78834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner} 78934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner 790a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx, 791a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar const ObjCInterfaceDecl *OI, 792a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar llvm::SmallVectorImpl<FieldDecl*> &Fields) { 793a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 794a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian E = OI->ivar_end(); I != E; ++I) { 795f1690858344968358131f8d5690d9ee458883000Chris Lattner ObjCIvarDecl *IVDecl = *I; 796a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian if (!IVDecl->isInvalidDecl()) 797a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian Fields.push_back(cast<FieldDecl>(IVDecl)); 798a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian } 799a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian} 800a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian 801a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, 802a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar llvm::SmallVectorImpl<FieldDecl*> &Fields) { 803a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) 804a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar CollectObjCIvars(SuperClass, Fields); 805a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar CollectLocalObjCIvars(this, OI, Fields); 806a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar} 807a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar 8088e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars - 8098e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class. 8108e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// 8118e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 8128e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, 8138e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian bool CollectSynthesized) { 8148e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 8158e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian E = OI->ivar_end(); I != E; ++I) { 8168e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian Ivars.push_back(*I); 8178e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian } 8188e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if (CollectSynthesized) 8198e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian CollectSynthesizedIvars(OI, Ivars); 8208e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian} 8218e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8229820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 8239820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { 82417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), 82517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = PD->prop_end(); I != E; ++I) 8269820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) 8279820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian Ivars.push_back(Ivar); 8289820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian 8299820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian // Also look into nested protocols. 8309820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), 8319820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian E = PD->protocol_end(); P != E; ++P) 8329820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian CollectProtocolSynthesizedIvars(*P, Ivars); 8339820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian} 8349820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian 8359820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// CollectSynthesizedIvars - 8369820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// This routine collect synthesized ivars for the designated class. 8379820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// 8389820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, 8399820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { 84017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), 84117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = OI->prop_end(); I != E; ++I) { 8429820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) 8439820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian Ivars.push_back(Ivar); 8449820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian } 8459820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian // Also look into interface's protocol list for properties declared 8469820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian // in the protocol and whose ivars are synthesized. 8479820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), 8489820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian PE = OI->protocol_end(); P != PE; ++P) { 8499820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian ObjCProtocolDecl *PD = (*P); 8509820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian CollectProtocolSynthesizedIvars(PD, Ivars); 8519820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian } 8529820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian} 8539820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian 8548e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { 8558e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian unsigned count = 0; 85617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), 85717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = PD->prop_end(); I != E; ++I) 8588e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if ((*I)->getPropertyIvarDecl()) 8598e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian ++count; 8608e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8618e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian // Also look into nested protocols. 8628e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), 8638e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian E = PD->protocol_end(); P != E; ++P) 8648e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian count += CountProtocolSynthesizedIvars(*P); 8658e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian return count; 8668e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian} 8678e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8688e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) 8698e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian{ 8708e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian unsigned count = 0; 87117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), 87217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = OI->prop_end(); I != E; ++I) { 8738e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if ((*I)->getPropertyIvarDecl()) 8748e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian ++count; 8758e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian } 8768e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian // Also look into interface's protocol list for properties declared 8778e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian // in the protocol and whose ivars are synthesized. 8788e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), 8798e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian PE = OI->protocol_end(); P != PE; ++P) { 8808e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian ObjCProtocolDecl *PD = (*P); 8818e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian count += CountProtocolSynthesizedIvars(PD); 8828e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian } 8838e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian return count; 8848e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian} 8858e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8868a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 8878a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { 8888a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator 8898a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis I = ObjCImpls.find(D); 8908a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis if (I != ObjCImpls.end()) 8918a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return cast<ObjCImplementationDecl>(I->second); 8928a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return 0; 8938a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 8948a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 8958a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { 8968a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator 8978a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis I = ObjCImpls.find(D); 8988a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis if (I != ObjCImpls.end()) 8998a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return cast<ObjCCategoryImplDecl>(I->second); 9008a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return 0; 9018a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 9028a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis 9038a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl. 9048a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, 9058a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCImplementationDecl *ImplD) { 9068a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis assert(IFaceD && ImplD && "Passed null params"); 9078a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCImpls[IFaceD] = ImplD; 9088a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 9098a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl. 9108a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, 9118a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCCategoryImplDecl *ImplD) { 9128a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis assert(CatD && ImplD && "Passed null params"); 9138a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCImpls[CatD] = ImplD; 9148a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 9158a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis 916b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Allocate an uninitialized DeclaratorInfo. 917b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// 918b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// The caller should initialize the memory held by DeclaratorInfo using 919b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers. 920b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// 921b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type 922b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to 923b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to. 924b17166c8077cd900cca83a895c43b30ea6660598Argyrios KyrtzidisDeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) { 925b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis unsigned DataSize = TypeLoc::getFullDataSizeForType(T); 926b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis DeclaratorInfo *DInfo = 927b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8); 928b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis new (DInfo) DeclaratorInfo(T); 929b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis return DInfo; 930b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis} 931b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis 932b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the 933b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface. 934b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// 935b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's 936b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars. 93744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout & 938b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D, 939b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar const ObjCImplementationDecl *Impl) { 940532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar assert(!D->isForwardDecl() && "Invalid interface decl!"); 941532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar 94244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel // Look up this layout, if already laid out, return what we have. 943d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar ObjCContainerDecl *Key = 944d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; 945d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) 946d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar return *Entry; 94744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel 948453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // Add in synthesized ivar count if laying out an implementation. 949453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar if (Impl) { 95029445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson unsigned FieldCount = D->ivar_size(); 9518e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian unsigned SynthCount = CountSynthesizedIvars(D); 9528e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian FieldCount += SynthCount; 953d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar // If there aren't any sythesized ivars then reuse the interface 954453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // entry. Note we can't cache this because we simply free all 955453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // entries later; however we shouldn't look up implementations 956453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // frequently. 9578e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if (SynthCount == 0) 958453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar return getObjCLayout(D, 0); 959453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar } 960453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar 96129445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson const ASTRecordLayout *NewEntry = 96229445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); 96329445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson ObjCLayouts[Key] = NewEntry; 96429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson 96544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel return *NewEntry; 96644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel} 96744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel 968b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout & 969b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { 970b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar return getObjCLayout(D, 0); 971b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar} 972b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar 973b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout & 974b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { 975b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar return getObjCLayout(D->getClassInterface(), D); 976b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar} 977b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar 97888a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the 979464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field 980464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information. 98198be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { 9824b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek D = D->getDefinition(*this); 9834b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek assert(D && "Cannot get layout of forward declarations!"); 9844bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman 985464175bba1318bef7905122e9fda20cff926df78Chris Lattner // Look up this layout, if already laid out, return what we have. 986ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman // Note that we can't save a reference to the entry because this function 987ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman // is recursive. 988ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman const ASTRecordLayout *Entry = ASTRecordLayouts[D]; 989464175bba1318bef7905122e9fda20cff926df78Chris Lattner if (Entry) return *Entry; 9904bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman 99129445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson const ASTRecordLayout *NewEntry = 99229445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson ASTRecordLayoutBuilder::ComputeLayout(*this, D); 993ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman ASTRecordLayouts[D] = NewEntry; 99429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson 9955d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner return *NewEntry; 996464175bba1318bef7905122e9fda20cff926df78Chris Lattner} 997464175bba1318bef7905122e9fda20cff926df78Chris Lattner 998a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===// 999a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner// Type creation/memoization methods 1000a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===// 1001a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 1002f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { 1003f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner QualType CanT = getCanonicalType(T); 1004f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner if (CanT.getAddressSpace() == AddressSpace) 1005f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner return T; 1006b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 1007b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // If we are composing extended qualifiers together, merge together into one 1008b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // ExtQualType node. 1009b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner unsigned CVRQuals = T.getCVRQualifiers(); 1010b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner QualType::GCAttrTypes GCAttr = QualType::GCNone; 1011b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner Type *TypeNode = T.getTypePtr(); 1012b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 1013b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { 1014b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // If this type already has an address space specified, it cannot get 1015b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // another one. 1016b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner assert(EQT->getAddressSpace() == 0 && 1017b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner "Type cannot be in multiple addr spaces!"); 1018b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner GCAttr = EQT->getObjCGCAttr(); 1019b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner TypeNode = EQT->getBaseType(); 1020b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner } 1021f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner 1022b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // Check if we've already instantiated this type. 1023ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb llvm::FoldingSetNodeID ID; 10242455636163fdd18581d7fdae816433f886d88213Mike Stump ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); 1025ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb void *InsertPos = 0; 1026f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) 1027b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(EXTQy, CVRQuals); 1028b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 1029ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb // If the base type isn't canonical, this won't be a canonical type either, 1030ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb // so fill in the canonical type field. 1031ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb QualType Canonical; 1032b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (!TypeNode->isCanonical()) { 1033f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian Canonical = getAddrSpaceQualType(CanT, AddressSpace); 1034ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb 1035b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // Update InsertPos, the previous call could have invalidated it. 1036f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); 1037f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1038ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb } 1039b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner ExtQualType *New = 1040b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); 1041f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian ExtQualTypes.InsertNode(New, InsertPos); 1042ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb Types.push_back(New); 1043b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(New, CVRQuals); 1044ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb} 1045ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb 1046b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T, 1047b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner QualType::GCAttrTypes GCAttr) { 1048d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian QualType CanT = getCanonicalType(T); 1049b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (CanT.getObjCGCAttr() == GCAttr) 1050d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian return T; 1051d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 10524027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian if (T->isPointerType()) { 10536217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType Pointee = T->getAs<PointerType>()->getPointeeType(); 105458f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff if (Pointee->isAnyPointerType()) { 10554027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian QualType ResultType = getObjCGCQualType(Pointee, GCAttr); 10564027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian return getPointerType(ResultType); 10574027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian } 10584027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian } 1059b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // If we are composing extended qualifiers together, merge together into one 1060b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // ExtQualType node. 1061b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner unsigned CVRQuals = T.getCVRQualifiers(); 1062b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner Type *TypeNode = T.getTypePtr(); 1063b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner unsigned AddressSpace = 0; 1064b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 1065b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { 10662455636163fdd18581d7fdae816433f886d88213Mike Stump // If this type already has an ObjCGC specified, it cannot get 1067b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // another one. 1068b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner assert(EQT->getObjCGCAttr() == QualType::GCNone && 10692455636163fdd18581d7fdae816433f886d88213Mike Stump "Type cannot have multiple ObjCGCs!"); 1070b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner AddressSpace = EQT->getAddressSpace(); 1071b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner TypeNode = EQT->getBaseType(); 1072b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner } 1073d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 1074d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian // Check if we've already instantiated an gc qual'd type of this type. 1075d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian llvm::FoldingSetNodeID ID; 10762455636163fdd18581d7fdae816433f886d88213Mike Stump ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); 1077d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian void *InsertPos = 0; 1078d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) 1079b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(EXTQy, CVRQuals); 1080d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 1081d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian // If the base type isn't canonical, this won't be a canonical type either, 1082d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian // so fill in the canonical type field. 10835a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: Isn't this also not canonical if the base type is a array 10845a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // or pointer type? I can't find any documentation for objc_gc, though... 1085d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian QualType Canonical; 1086d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian if (!T->isCanonical()) { 1087b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner Canonical = getObjCGCQualType(CanT, GCAttr); 1088d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 1089b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // Update InsertPos, the previous call could have invalidated it. 1090d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); 1091d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1092d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian } 1093b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner ExtQualType *New = 1094b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); 1095d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian ExtQualTypes.InsertNode(New, InsertPos); 1096d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian Types.push_back(New); 1097b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(New, CVRQuals); 1098d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian} 1099a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 11002455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getNoReturnType(QualType T) { 11016dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump QualifierSet qs; 11026dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump qs.strip(T); 11032455636163fdd18581d7fdae816433f886d88213Mike Stump if (T->isPointerType()) { 11046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType Pointee = T->getAs<PointerType>()->getPointeeType(); 11052455636163fdd18581d7fdae816433f886d88213Mike Stump QualType ResultType = getNoReturnType(Pointee); 11066dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType = getPointerType(ResultType); 11076dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType.setCVRQualifiers(T.getCVRQualifiers()); 11086dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump return qs.apply(ResultType, *this); 11092455636163fdd18581d7fdae816433f886d88213Mike Stump } 11102455636163fdd18581d7fdae816433f886d88213Mike Stump if (T->isBlockPointerType()) { 11116217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType(); 11122455636163fdd18581d7fdae816433f886d88213Mike Stump QualType ResultType = getNoReturnType(Pointee); 11136dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType = getBlockPointerType(ResultType); 11146dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType.setCVRQualifiers(T.getCVRQualifiers()); 11156dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump return qs.apply(ResultType, *this); 11162455636163fdd18581d7fdae816433f886d88213Mike Stump } 11172455636163fdd18581d7fdae816433f886d88213Mike Stump if (!T->isFunctionType()) 11182455636163fdd18581d7fdae816433f886d88213Mike Stump assert(0 && "can't noreturn qualify non-pointer to function or block type"); 11192455636163fdd18581d7fdae816433f886d88213Mike Stump 1120e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump if (const FunctionNoProtoType *F = T->getAsFunctionNoProtoType()) { 11212455636163fdd18581d7fdae816433f886d88213Mike Stump return getFunctionNoProtoType(F->getResultType(), true); 11222455636163fdd18581d7fdae816433f886d88213Mike Stump } 1123e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump const FunctionProtoType *F = T->getAsFunctionProtoType(); 11242455636163fdd18581d7fdae816433f886d88213Mike Stump return getFunctionType(F->getResultType(), F->arg_type_begin(), 11252455636163fdd18581d7fdae816433f886d88213Mike Stump F->getNumArgs(), F->isVariadic(), F->getTypeQuals(), 11262455636163fdd18581d7fdae816433f886d88213Mike Stump F->hasExceptionSpec(), F->hasAnyExceptionSpec(), 11272455636163fdd18581d7fdae816433f886d88213Mike Stump F->getNumExceptions(), F->exception_begin(), true); 11282455636163fdd18581d7fdae816433f886d88213Mike Stump} 11292455636163fdd18581d7fdae816433f886d88213Mike Stump 11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex 11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type. 11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) { 11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique pointers, to guarantee there is only one pointer of a particular 11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ComplexType::Profile(ID, T); 11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) 11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(CT, 0); 11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the pointee type isn't canonical, this won't be a canonical type either, 11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!T->isCanonical()) { 1146f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getComplexType(getCanonicalType(T)); 11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); 1150f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1152f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff ComplexType *New = new (*this,8) ComplexType(T, Canonical); 11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ComplexTypes.InsertNode(New, InsertPos); 11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1158f98aba35e6c3da5aae61843fc01334939e4e12ecEli FriedmanQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { 1159f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? 1160f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; 1161f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman FixedWidthIntType *&Entry = Map[Width]; 1162f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman if (!Entry) 1163f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman Entry = new FixedWidthIntType(Width, Signed); 1164f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return QualType(Entry, 0); 1165f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman} 11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to 11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type. 11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) { 11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique pointers, to guarantee there is only one pointer of a particular 11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PointerType::Profile(ID, T); 11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(PT, 0); 11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the pointee type isn't canonical, this won't be a canonical type either, 11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!T->isCanonical()) { 1183f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getPointerType(getCanonicalType(T)); 11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1187f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1189f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff PointerType *New = new (*this,8) PointerType(T, Canonical); 11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PointerTypes.InsertNode(New, InsertPos); 11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11955618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// getBlockPointerType - Return the uniqued reference to the type for 11965618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block. 11975618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) { 1198296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff assert(T->isFunctionType() && "block of function types only"); 1199296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff // Unique pointers, to guarantee there is only one block of a particular 12005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff // structure. 12015618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff llvm::FoldingSetNodeID ID; 12025618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerType::Profile(ID, T); 12035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 12045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff void *InsertPos = 0; 12055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff if (BlockPointerType *PT = 12065618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 12075618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff return QualType(PT, 0); 12085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 1209296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff // If the block pointee type isn't canonical, this won't be a canonical 12105618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff // type either so fill in the canonical type field. 12115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff QualType Canonical; 12125618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff if (!T->isCanonical()) { 12135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff Canonical = getBlockPointerType(getCanonicalType(T)); 12145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 12155618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff // Get the new insert position for the node we care about. 12165618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerType *NewIP = 12175618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1218f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 12195618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff } 1220f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical); 12215618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff Types.push_back(New); 12225618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerTypes.InsertNode(New, InsertPos); 12235618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff return QualType(New, 0); 12245618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff} 12255618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 12267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an 12277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type. 12287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getLValueReferenceType(QualType T) { 12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique pointers, to guarantee there is only one pointer of a particular 12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ReferenceType::Profile(ID, T); 12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 12357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl if (LValueReferenceType *RT = 12367c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(RT, 0); 12387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the referencee type isn't canonical, this won't be a canonical type 12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // either, so fill in the canonical type field. 12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!T->isCanonical()) { 12437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl Canonical = getLValueReferenceType(getCanonicalType(T)); 12447c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // Get the new insert position for the node we care about. 12467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceType *NewIP = 12477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 12487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 12497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl } 12507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical); 12527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl Types.push_back(New); 12537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceTypes.InsertNode(New, InsertPos); 12547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl return QualType(New, 0); 12557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl} 12567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12577c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an 12587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type. 12597c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) { 12607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // Unique pointers, to guarantee there is only one pointer of a particular 12617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // structure. 12627c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl llvm::FoldingSetNodeID ID; 12637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl ReferenceType::Profile(ID, T); 12647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl void *InsertPos = 0; 12667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl if (RValueReferenceType *RT = 12677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 12687c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl return QualType(RT, 0); 12697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // If the referencee type isn't canonical, this won't be a canonical type 12717c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // either, so fill in the canonical type field. 12727c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl QualType Canonical; 12737c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl if (!T->isCanonical()) { 12747c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl Canonical = getRValueReferenceType(getCanonicalType(T)); 12757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 12777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceType *NewIP = 12787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 1279f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical); 12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 12847c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceTypes.InsertNode(New, InsertPos); 12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1288f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a 1289f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class. 1290f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian RedlQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) 1291f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl{ 1292f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // Unique pointers, to guarantee there is only one pointer of a particular 1293f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // structure. 1294f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl llvm::FoldingSetNodeID ID; 1295f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerType::Profile(ID, T, Cls); 1296f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1297f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl void *InsertPos = 0; 1298f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl if (MemberPointerType *PT = 1299f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1300f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl return QualType(PT, 0); 1301f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1302f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // If the pointee or class type isn't canonical, this won't be a canonical 1303f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // type either, so fill in the canonical type field. 1304f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl QualType Canonical; 1305f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl if (!T->isCanonical()) { 1306f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); 1307f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1308f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // Get the new insert position for the node we care about. 1309f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerType *NewIP = 1310f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1311f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1312f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl } 1313f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical); 1314f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl Types.push_back(New); 1315f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerTypes.InsertNode(New, InsertPos); 1316f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl return QualType(New, 0); 1317f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl} 1318f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1319fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// getConstantArrayType - Return the unique reference to the type for an 1320fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type. 1321fb22d96692c5240fb8d611290dbf7eeed3759c73Steve NaroffQualType ASTContext::getConstantArrayType(QualType EltTy, 132238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner const llvm::APInt &ArySizeIn, 1323c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff ArrayType::ArraySizeModifier ASM, 1324c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff unsigned EltTypeQuals) { 1325587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) && 1326587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman "Constant array of VLAs is illegal!"); 1327587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman 132838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner // Convert the array size into a canonical width matching the pointer size for 132938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner // the target. 133038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner llvm::APInt ArySize(ArySizeIn); 133138aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); 133238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner 13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 13340be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); 13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 13377192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek if (ConstantArrayType *ATP = 13387192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(ATP, 0); 13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the element type isn't canonical, this won't be a canonical type either, 13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!EltTy->isCanonical()) { 1345f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, 1346c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff ASM, EltTypeQuals); 13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 13487192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayType *NewIP = 13497192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1350f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1353566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek ConstantArrayType *New = 1354f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); 13557192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayTypes.InsertNode(New, InsertPos); 13565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 13575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithExprType - Return a reference to the type for 13617e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type. 13627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType 13637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithExprType(QualType EltTy, 13647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor const llvm::APInt &ArySizeIn, 13657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Expr *ArySizeExpr, 13667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArrayType::ArraySizeModifier ASM, 13677e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals, 13687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor SourceRange Brackets) { 13697e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Convert the array size into a canonical width matching the pointer 13707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // size for the target. 13717e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor llvm::APInt ArySize(ArySizeIn); 13727e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); 13737e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 13747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Compute the canonical ConstantArrayType. 13757e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor QualType Canonical = getConstantArrayType(getCanonicalType(EltTy), 13767e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ASM, EltTypeQuals); 13777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Since we don't unique expressions, it isn't possible to unique VLA's 13787e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // that have an expression provided for their size. 13797e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ConstantArrayWithExprType *New = 13807e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor new(*this,8)ConstantArrayWithExprType(EltTy, Canonical, 13817e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ArySizeExpr, 13827e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ASM, EltTypeQuals, Brackets); 13837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Types.push_back(New); 13847e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return QualType(New, 0); 13857e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor} 13867e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 13877e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithoutExprType - Return a reference to the type for 13887e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type. 13897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType 13907e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithoutExprType(QualType EltTy, 13917e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor const llvm::APInt &ArySizeIn, 13927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArrayType::ArraySizeModifier ASM, 13937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals) { 13947e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Convert the array size into a canonical width matching the pointer 13957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // size for the target. 13967e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor llvm::APInt ArySize(ArySizeIn); 13977e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); 13987e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 13997e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Compute the canonical ConstantArrayType. 14007e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor QualType Canonical = getConstantArrayType(getCanonicalType(EltTy), 14017e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ASM, EltTypeQuals); 14027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ConstantArrayWithoutExprType *New = 14037e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical, 14047e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ASM, EltTypeQuals); 14057e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Types.push_back(New); 14067e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return QualType(New, 0); 14077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor} 14087e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 1409bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a 1410bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type. 14117e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy, 14127e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Expr *NumElts, 1413c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff ArrayType::ArraySizeModifier ASM, 14147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals, 14157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor SourceRange Brackets) { 1416c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // Since we don't unique expressions, it isn't possible to unique VLA's 1417c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // that have an expression provided for their size. 1418c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1419566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek VariableArrayType *New = 14207e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor new(*this,8)VariableArrayType(EltTy, QualType(), 14217e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor NumElts, ASM, EltTypeQuals, Brackets); 1422c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1423c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman VariableArrayTypes.push_back(New); 1424c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman Types.push_back(New); 1425c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman return QualType(New, 0); 1426c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman} 1427c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1428898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to 1429898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element 143004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type. 14317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy, 14327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Expr *NumElts, 1433898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor ArrayType::ArraySizeModifier ASM, 14347e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals, 14357e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor SourceRange Brackets) { 1436898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && 1437898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor "Size must be type- or value-dependent!"); 1438898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 143904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor llvm::FoldingSetNodeID ID; 144004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, 144104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor EltTypeQuals, NumElts); 1442898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 144304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor void *InsertPos = 0; 144404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayType *Canon 144504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 144604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayType *New; 144704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor if (Canon) { 144804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor // We already have a canonical version of this array type; use it as 144904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor // the canonical type for a newly-built type. 145004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor New = new (*this,8) DependentSizedArrayType(*this, EltTy, 145104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor QualType(Canon, 0), 145204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor NumElts, ASM, EltTypeQuals, 145304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor Brackets); 145404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } else { 145504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor QualType CanonEltTy = getCanonicalType(EltTy); 145604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor if (CanonEltTy == EltTy) { 145704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor New = new (*this,8) DependentSizedArrayType(*this, EltTy, QualType(), 145804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor NumElts, ASM, EltTypeQuals, 145904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor Brackets); 146004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayTypes.InsertNode(New, InsertPos); 146104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } else { 146204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, 146304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor ASM, EltTypeQuals, 146404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor SourceRange()); 146504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor New = new (*this,8) DependentSizedArrayType(*this, EltTy, Canon, 146604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor NumElts, ASM, EltTypeQuals, 146704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor Brackets); 146804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } 146904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } 147004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor 1471898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor Types.push_back(New); 1472898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return QualType(New, 0); 1473898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor} 1474898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 1475c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy, 1476c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman ArrayType::ArraySizeModifier ASM, 1477c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman unsigned EltTypeQuals) { 1478c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman llvm::FoldingSetNodeID ID; 14790be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); 1480c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1481c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman void *InsertPos = 0; 1482c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman if (IncompleteArrayType *ATP = 1483c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 1484c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman return QualType(ATP, 0); 1485c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1486c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // If the element type isn't canonical, this won't be a canonical type 1487c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // either, so fill in the canonical type field. 1488c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman QualType Canonical; 1489c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1490c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman if (!EltTy->isCanonical()) { 1491f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getIncompleteArrayType(getCanonicalType(EltTy), 14922bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek ASM, EltTypeQuals); 1493c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1494c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // Get the new insert position for the node we care about. 1495c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayType *NewIP = 1496c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1497f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 14982bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek } 1499c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 15007e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor IncompleteArrayType *New 15017e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor = new (*this,8) IncompleteArrayType(EltTy, Canonical, 15027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ASM, EltTypeQuals); 1503c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1504c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayTypes.InsertNode(New, InsertPos); 1505c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman Types.push_back(New); 1506c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman return QualType(New, 0); 1507fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff} 1508fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff 150973322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of 151073322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type. 151173322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { 15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BuiltinType *baseType; 15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1514f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 151573322924127c873c13101b705dd823f5539ffa5fSteve Naroff assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); 15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Check if we've already instantiated a vector of this type. 15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 151973322924127c873c13101b705dd823f5539ffa5fSteve Naroff VectorType::Profile(ID, vecType, NumElts, Type::Vector); 15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(VTP, 0); 15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the element type isn't canonical, this won't be a canonical type either, 15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!vecType->isCanonical()) { 1528f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getVectorType(getCanonicalType(vecType), NumElts); 15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1532f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1534f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical); 15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer VectorTypes.InsertNode(New, InsertPos); 15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 15385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1540213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of 154173322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type. 1542213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { 154373322924127c873c13101b705dd823f5539ffa5fSteve Naroff BuiltinType *baseType; 154473322924127c873c13101b705dd823f5539ffa5fSteve Naroff 1545f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 1546213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); 154773322924127c873c13101b705dd823f5539ffa5fSteve Naroff 154873322924127c873c13101b705dd823f5539ffa5fSteve Naroff // Check if we've already instantiated a vector of this type. 154973322924127c873c13101b705dd823f5539ffa5fSteve Naroff llvm::FoldingSetNodeID ID; 1550213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); 155173322924127c873c13101b705dd823f5539ffa5fSteve Naroff void *InsertPos = 0; 155273322924127c873c13101b705dd823f5539ffa5fSteve Naroff if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 155373322924127c873c13101b705dd823f5539ffa5fSteve Naroff return QualType(VTP, 0); 155473322924127c873c13101b705dd823f5539ffa5fSteve Naroff 155573322924127c873c13101b705dd823f5539ffa5fSteve Naroff // If the element type isn't canonical, this won't be a canonical type either, 155673322924127c873c13101b705dd823f5539ffa5fSteve Naroff // so fill in the canonical type field. 155773322924127c873c13101b705dd823f5539ffa5fSteve Naroff QualType Canonical; 155873322924127c873c13101b705dd823f5539ffa5fSteve Naroff if (!vecType->isCanonical()) { 1559213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); 156073322924127c873c13101b705dd823f5539ffa5fSteve Naroff 156173322924127c873c13101b705dd823f5539ffa5fSteve Naroff // Get the new insert position for the node we care about. 156273322924127c873c13101b705dd823f5539ffa5fSteve Naroff VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1563f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 156473322924127c873c13101b705dd823f5539ffa5fSteve Naroff } 1565f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical); 156673322924127c873c13101b705dd823f5539ffa5fSteve Naroff VectorTypes.InsertNode(New, InsertPos); 156773322924127c873c13101b705dd823f5539ffa5fSteve Naroff Types.push_back(New); 156873322924127c873c13101b705dd823f5539ffa5fSteve Naroff return QualType(New, 0); 156973322924127c873c13101b705dd823f5539ffa5fSteve Naroff} 157073322924127c873c13101b705dd823f5539ffa5fSteve Naroff 15719cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas GregorQualType ASTContext::getDependentSizedExtVectorType(QualType vecType, 15729cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor Expr *SizeExpr, 15739cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor SourceLocation AttrLoc) { 15742ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor llvm::FoldingSetNodeID ID; 15752ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), 15762ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SizeExpr); 15772ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor 15782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor void *InsertPos = 0; 15792ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorType *Canon 15802ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); 15812ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorType *New; 15822ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor if (Canon) { 15832ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor // We already have a canonical version of this array type; use it as 15842ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor // the canonical type for a newly-built type. 15852ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor New = new (*this,8) DependentSizedExtVectorType(*this, vecType, 15862ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType(Canon, 0), 15872ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SizeExpr, AttrLoc); 15882ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } else { 15892ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType CanonVecTy = getCanonicalType(vecType); 15902ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor if (CanonVecTy == vecType) { 15912ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor New = new (*this,8) DependentSizedExtVectorType(*this, vecType, 15922ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType(), SizeExpr, 15932ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor AttrLoc); 15942ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorTypes.InsertNode(New, InsertPos); 15952ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } else { 15962ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, 15972ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SourceLocation()); 15982ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor New = new (*this,8) DependentSizedExtVectorType(*this, vecType, Canon, 15992ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SizeExpr, AttrLoc); 16002ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } 16012ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } 16022ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor 16039cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor Types.push_back(New); 16049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor return QualType(New, 0); 16059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor} 16069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor 160772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 16092455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { 16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique functions, to guarantee there is only one function of a particular 16115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 16132455636163fdd18581d7fdae816433f886d88213Mike Stump FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); 16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 161672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor if (FunctionNoProtoType *FT = 161772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(FT, 0); 16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!ResultTy->isCanonical()) { 16222455636163fdd18581d7fdae816433f886d88213Mike Stump Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); 16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 162572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoType *NewIP = 162672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1627f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 16285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16302455636163fdd18581d7fdae816433f886d88213Mike Stump FunctionNoProtoType *New 16312455636163fdd18581d7fdae816433f886d88213Mike Stump = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn); 16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 163372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoTypes.InsertNode(New, InsertPos); 16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 16355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument 16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list. isVariadic indicates whether the argument list includes '...'. 163961710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, 1640971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis unsigned NumArgs, bool isVariadic, 1641465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl unsigned TypeQuals, bool hasExceptionSpec, 1642465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl bool hasAnyExceptionSpec, unsigned NumExs, 16432455636163fdd18581d7fdae816433f886d88213Mike Stump const QualType *ExArray, bool NoReturn) { 16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique functions, to guarantee there is only one function of a particular 16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 164772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, 1648465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, 16492455636163fdd18581d7fdae816433f886d88213Mike Stump NumExs, ExArray, NoReturn); 16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 165272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor if (FunctionProtoType *FTP = 165372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 16545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(FTP, 0); 1655465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 1656465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // Determine whether the type being created is already canonical or not. 16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isCanonical = ResultTy->isCanonical(); 1658465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl if (hasExceptionSpec) 1659465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl isCanonical = false; 16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; i != NumArgs && isCanonical; ++i) 16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!ArgArray[i]->isCanonical()) 16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer isCanonical = false; 16635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this type isn't canonical, get the canonical version of it. 1665465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // The exception spec is not part of the canonical type. 16665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 16675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isCanonical) { 16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::SmallVector<QualType, 16> CanonicalArgs; 16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CanonicalArgs.reserve(NumArgs); 16705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; i != NumArgs; ++i) 1671f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); 1672465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 1673f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getFunctionType(getCanonicalType(ResultTy), 1674beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad CanonicalArgs.data(), NumArgs, 167547259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor isVariadic, TypeQuals, false, 167647259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor false, 0, 0, NoReturn); 1677465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 167972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoType *NewIP = 168072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1681f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 16825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1683465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 168472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor // FunctionProtoType objects are allocated with extra bytes after them 1685465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // for two variable size arrays (for parameter and exception types) at the 1686465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // end of them. 168772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoType *FTP = 1688465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + 1689465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl NumArgs*sizeof(QualType) + 1690465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl NumExs*sizeof(QualType), 8); 169172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, 1692465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, 16932455636163fdd18581d7fdae816433f886d88213Mike Stump ExArray, NumExs, Canonical, NoReturn); 16945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(FTP); 169572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoTypes.InsertNode(FTP, InsertPos); 16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(FTP, 0); 16975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16992ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the 17002ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration. 17014b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { 17021e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis assert(Decl && "Passed null for Decl param"); 17032ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 17042ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor 17051e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) 17062ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor return getTypedefType(Typedef); 1707fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor else if (isa<TemplateTypeParmDecl>(Decl)) { 1708fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor assert(false && "Template type parameter types are always available."); 17099fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else if (ObjCInterfaceDecl *ObjCInterface 17109fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump = dyn_cast<ObjCInterfaceDecl>(Decl)) 17112ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor return getObjCInterfaceType(ObjCInterface); 171249aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis 1713c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { 1714566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek if (PrevDecl) 1715566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek Decl->TypeForDecl = PrevDecl->TypeForDecl; 1716f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff else 1717f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff Decl->TypeForDecl = new (*this,8) RecordType(Record); 17189fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { 1719566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek if (PrevDecl) 1720566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek Decl->TypeForDecl = PrevDecl->TypeForDecl; 1721f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff else 1722f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff Decl->TypeForDecl = new (*this,8) EnumType(Enum); 17239fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else 17242ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor assert(false && "TypeDecl without a type?"); 172549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis 17264b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek if (!PrevDecl) Types.push_back(Decl->TypeForDecl); 172749aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis return QualType(Decl->TypeForDecl, 0); 17282ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor} 17292ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor 17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the 17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl. 17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) { 17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1735f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); 173672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical); 17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(Decl->TypeForDecl); 17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(Decl->TypeForDecl, 0); 17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1741fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template 174276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// parameter or parameter pack with the given depth, index, and (optionally) 174376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name. 1744fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas GregorQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, 174576e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson bool ParameterPack, 1746fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor IdentifierInfo *Name) { 1747fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor llvm::FoldingSetNodeID ID; 174876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); 1749fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor void *InsertPos = 0; 1750fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor TemplateTypeParmType *TypeParm 1751fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); 1752fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 1753fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor if (TypeParm) 1754fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor return QualType(TypeParm, 0); 1755fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 175676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson if (Name) { 175776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); 175876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack, 175976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson Name, Canon); 176076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson } else 176176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack); 1762fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 1763fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor Types.push_back(TypeParm); 1764fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); 1765fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 1766fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor return QualType(TypeParm, 0); 1767fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor} 1768fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 176955f6b14230c94272efbbcdd89a92224c8db9f225Douglas GregorQualType 17707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template, 17717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor const TemplateArgument *Args, 17727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor unsigned NumArgs, 17737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualType Canon) { 1774b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor if (!Canon.isNull()) 1775b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor Canon = getCanonicalType(Canon); 1776b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor else { 1777b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor // Build the canonical template specialization type. 17781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateName CanonTemplate = getCanonicalTemplateName(Template); 17791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor llvm::SmallVector<TemplateArgument, 4> CanonArgs; 17801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs.reserve(NumArgs); 17811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 17821275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); 17831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 17841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Determine whether this canonical template specialization type already 17851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // exists. 17861275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor llvm::FoldingSetNodeID ID; 17871275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationType::Profile(ID, CanonTemplate, 1788828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor CanonArgs.data(), NumArgs, *this); 17891275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 17901275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor void *InsertPos = 0; 17911275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationType *Spec 17921275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); 17931275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 17941275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor if (!Spec) { 17951275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Allocate a new canonical template specialization type. 17961275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor void *Mem = Allocate((sizeof(TemplateSpecializationType) + 17971275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor sizeof(TemplateArgument) * NumArgs), 17981275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 8); 1799828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, 18001275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs.data(), NumArgs, 1801b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor Canon); 18021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor Types.push_back(Spec); 18031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationTypes.InsertNode(Spec, InsertPos); 18041275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor } 18051275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 1806b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor if (Canon.isNull()) 1807b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor Canon = QualType(Spec, 0); 18081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor assert(Canon->isDependentType() && 18091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor "Non-dependent template-id type must have a canonical type"); 1810b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor } 1811fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor 18121275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Allocate the (non-canonical) template specialization type, but don't 18131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // try to unique it: these types typically have location information that 18141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // we don't unique and don't want to lose. 18157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor void *Mem = Allocate((sizeof(TemplateSpecializationType) + 181640808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor sizeof(TemplateArgument) * NumArgs), 181740808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor 8); 18181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationType *Spec 1819828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, 1820828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor Canon); 18211275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 182255f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor Types.push_back(Spec); 182355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor return QualType(Spec, 0); 182455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor} 182555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor 1826e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas GregorQualType 1827ab452ba8323d1985e08bade2bced588cddf2cc28Douglas GregorASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, 1828e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor QualType NamedType) { 1829e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor llvm::FoldingSetNodeID ID; 1830ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor QualifiedNameType::Profile(ID, NNS, NamedType); 1831e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor 1832e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor void *InsertPos = 0; 1833e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor QualifiedNameType *T 1834e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); 1835e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor if (T) 1836e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor return QualType(T, 0); 1837e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor 1838ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor T = new (*this) QualifiedNameType(NNS, NamedType, 1839ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor getCanonicalType(NamedType)); 1840e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor Types.push_back(T); 1841e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor QualifiedNameTypes.InsertNode(T, InsertPos); 1842e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor return QualType(T, 0); 1843e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor} 1844e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor 1845d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, 1846d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor const IdentifierInfo *Name, 1847d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor QualType Canon) { 1848d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor assert(NNS->isDependent() && "nested-name-specifier must be dependent"); 1849d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1850d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (Canon.isNull()) { 1851d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 1852d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (CanonNNS != NNS) 1853d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor Canon = getTypenameType(CanonNNS, Name); 1854d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 1855d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1856d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor llvm::FoldingSetNodeID ID; 1857d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor TypenameType::Profile(ID, NNS, Name); 1858d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1859d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor void *InsertPos = 0; 1860d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor TypenameType *T 1861d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); 1862d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (T) 1863d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return QualType(T, 0); 1864d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1865d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor T = new (*this) TypenameType(NNS, Name, Canon); 1866d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor Types.push_back(T); 1867d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor TypenameTypes.InsertNode(T, InsertPos); 1868d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return QualType(T, 0); 1869d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor} 1870d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 18711734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorQualType 18721734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorASTContext::getTypenameType(NestedNameSpecifier *NNS, 18731734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor const TemplateSpecializationType *TemplateId, 18741734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor QualType Canon) { 18751734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor assert(NNS->isDependent() && "nested-name-specifier must be dependent"); 18761734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18771734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor if (Canon.isNull()) { 18781734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 18791734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); 18801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { 18811734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor const TemplateSpecializationType *CanonTemplateId 18821734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor = CanonType->getAsTemplateSpecializationType(); 18831734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor assert(CanonTemplateId && 18841734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor "Canonical type must also be a template specialization type"); 18851734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Canon = getTypenameType(CanonNNS, CanonTemplateId); 18861734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } 18871734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } 18881734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18891734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor llvm::FoldingSetNodeID ID; 18901734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor TypenameType::Profile(ID, NNS, TemplateId); 18911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18921734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor void *InsertPos = 0; 18931734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor TypenameType *T 18941734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); 18951734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor if (T) 18961734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor return QualType(T, 0); 18971734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18981734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor T = new (*this) TypenameType(NNS, TemplateId, Canon); 18991734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Types.push_back(T); 19001734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor TypenameTypes.InsertNode(T, InsertPos); 19011734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor return QualType(T, 0); 19021734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor} 19031734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 190488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols 190588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically. 190688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS, 190788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner const ObjCProtocolDecl *RHS) { 19082e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor return LHS->getDeclName() < RHS->getDeclName(); 190988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner} 191088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 191188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, 191288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner unsigned &NumProtocols) { 191388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; 191488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 191588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner // Sort protocols, keyed by name. 191688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); 191788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 191888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner // Remove duplicates. 191988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); 192088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner NumProtocols = ProtocolsEnd-Protocols; 192188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner} 192288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 1923d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for 1924d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// the given interface decl and the conforming protocol list. 192514108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, 1926d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCProtocolDecl **Protocols, 1927d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff unsigned NumProtocols) { 1928d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff // Sort the protocol list alphabetically to canonicalize it. 1929d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff if (NumProtocols) 1930d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff SortAndUniqueProtocols(Protocols, NumProtocols); 1931d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1932d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff llvm::FoldingSetNodeID ID; 193314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); 1934d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1935d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff void *InsertPos = 0; 1936d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff if (ObjCObjectPointerType *QT = 1937d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1938d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff return QualType(QT, 0); 1939d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1940d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff // No Match; 1941d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCObjectPointerType *QType = 194214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols); 1943d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1944d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff Types.push_back(QType); 1945d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCObjectPointerTypes.InsertNode(QType, InsertPos); 1946d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff return QualType(QType, 0); 1947d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff} 194888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 1949c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// getObjCInterfaceType - Return the unique reference to the type for the 1950c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// specified ObjC interface decl. The list of protocols is optional. 1951c15cb2af27514ecc879daba9aa01389c5203685dSteve NaroffQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 1952a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCProtocolDecl **Protocols, unsigned NumProtocols) { 1953c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (NumProtocols) 1954c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff // Sort the protocol list alphabetically to canonicalize it. 1955c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff SortAndUniqueProtocols(Protocols, NumProtocols); 195688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 19574b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian llvm::FoldingSetNodeID ID; 1958c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); 19594b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian 19604b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian void *InsertPos = 0; 1961c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (ObjCInterfaceType *QT = 1962c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) 19634b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian return QualType(QT, 0); 19644b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian 19654b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian // No Match; 1966c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceType *QType = 1967c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl), 1968c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff Protocols, NumProtocols); 19694b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian Types.push_back(QType); 1970c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceTypes.InsertNode(QType, InsertPos); 19714b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian return QualType(QType, 0); 19724b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian} 19734b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian 197472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique 197572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example, 19769752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different 19779752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// DeclRefExpr's. This doesn't effect the type checker, since it operates 19789752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique). 197972564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) { 1980dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor TypeOfExprType *toe; 1981b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor if (tofExpr->isTypeDependent()) { 1982b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor llvm::FoldingSetNodeID ID; 1983b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor DependentTypeOfExprType::Profile(ID, *this, tofExpr); 1984b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor 1985b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor void *InsertPos = 0; 1986b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor DependentTypeOfExprType *Canon 1987b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); 1988b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor if (Canon) { 1989b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor // We already have a "canonical" version of an identical, dependent 1990b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor // typeof(expr) type. Use that as our canonical type. 1991b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor toe = new (*this, 8) TypeOfExprType(tofExpr, 1992b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor QualType((TypeOfExprType*)Canon, 0)); 1993b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor } 1994b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor else { 1995b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor // Build a new, canonical typeof(expr) type. 1996b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr); 1997b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); 1998b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor toe = Canon; 1999b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor } 2000b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor } else { 2001dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor QualType Canonical = getCanonicalType(tofExpr->getType()); 2002dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor toe = new (*this,8) TypeOfExprType(tofExpr, Canonical); 2003dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor } 20049752f25748d954df99087d741ea35db37ff16beaSteve Naroff Types.push_back(toe); 20059752f25748d954df99087d741ea35db37ff16beaSteve Naroff return QualType(toe, 0); 2006d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff} 2007d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff 20089752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique 20099752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be 20109752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be 20119752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// an issue. This doesn't effect the type checker, since it operates 20129752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique). 2013d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) { 2014f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner QualType Canonical = getCanonicalType(tofType); 2015f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical); 20169752f25748d954df99087d741ea35db37ff16beaSteve Naroff Types.push_back(tot); 20179752f25748d954df99087d741ea35db37ff16beaSteve Naroff return QualType(tot, 0); 2018d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff} 2019d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff 202060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that 202160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4 202260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { 2023a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson if (e->isTypeDependent()) 2024a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson return Context.DependentTy; 2025a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson 202660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // If e is an id expression or a class member access, decltype(e) is defined 202760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // as the type of the entity named by e. 202860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { 202960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) 203060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return VD->getType(); 203160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson } 203260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { 203360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) 203460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return FD->getType(); 203560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson } 203660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // If e is a function call or an invocation of an overloaded operator, 203760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // (parentheses around e are ignored), decltype(e) is defined as the 203860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // return type of that function. 203960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) 204060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return CE->getCallReturnType(); 204160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 204260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson QualType T = e->getType(); 204360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 204460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is 204560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // defined as T&, otherwise decltype(e) is defined as T. 204660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (e->isLvalue(Context) == Expr::LV_Valid) 204760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson T = Context.getLValueReferenceType(T); 204860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 204960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return T; 205060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson} 205160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 2052395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique 2053395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be 2054395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be 2055395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// an issue. This doesn't effect the type checker, since it operates 2056395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique). 2057395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) { 2058dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor DecltypeType *dt; 20599d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor if (e->isTypeDependent()) { 20609d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor llvm::FoldingSetNodeID ID; 20619d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor DependentDecltypeType::Profile(ID, *this, e); 20629d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor 20639d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor void *InsertPos = 0; 20649d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor DependentDecltypeType *Canon 20659d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); 20669d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor if (Canon) { 20679d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor // We already have a "canonical" version of an equivalent, dependent 20689d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor // decltype type. Use that as our canonical type. 20699d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor dt = new (*this, 8) DecltypeType(e, DependentTy, 20709d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor QualType((DecltypeType*)Canon, 0)); 20719d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor } 20729d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor else { 20739d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor // Build a new, canonical typeof(expr) type. 20749d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor Canon = new (*this, 8) DependentDecltypeType(*this, e); 20759d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor DependentDecltypeTypes.InsertNode(Canon, InsertPos); 20769d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor dt = Canon; 20779d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor } 20789d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor } else { 2079dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor QualType T = getDecltypeForExpr(e, *this); 2080563a03b1338d31c2462def43253a722bc885d384Anders Carlsson dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T)); 2081dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor } 2082395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson Types.push_back(dt); 2083395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson return QualType(dt, 0); 2084395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson} 2085395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson 20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the 20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl. 2088e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) { 2089d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek assert (Decl); 2090e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump // FIXME: What is the design on getTagDeclType when it requires casting 2091e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump // away const? mutable? 2092e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump return getTypeDeclType(const_cast<TagDecl*>(Decl)); 20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result 20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and 20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// needs to agree with the definition in <stddef.h>. 20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const { 2099b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor return getFromTargetType(Target.getSizeType()); 21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 210264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t". 210364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension. 210464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const { 210564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis // FIXME: derive from "Target" ? 210664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis return WCharTy; 210764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis} 210864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis 210964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t". 211064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension. 211164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const { 211264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis // FIXME: derive from "Target" ? 211364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis return UnsignedIntTy; 211464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis} 211564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis 21168b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 21178b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 21188b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const { 2119b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor return getFromTargetType(Target.getPtrDiffType(0)); 21208b9023ba35a86838789e2c9034a6128728c547aaChris Lattner} 21218b9023ba35a86838789e2c9034a6128728c547aaChris Lattner 2122e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===// 2123e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner// Type Operators 2124e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===// 2125e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 212677c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to 212777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type. The non-canonical version 212877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types. Decorators can 212977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 213077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared 213177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison. 213250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) { 213377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); 2134c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2135c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If the result has type qualifiers, make sure to canonicalize them as well. 2136c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); 213750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor if (TypeQuals == 0) 213850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe(CanType); 2139c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2140c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If the type qualifiers are on an array type, get the canonical type of the 2141c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // array with the qualifiers applied to the element type. 2142c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner ArrayType *AT = dyn_cast<ArrayType>(CanType); 2143c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (!AT) 214450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe(CanType.getQualifiedType(TypeQuals)); 2145c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2146c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Get the canonical version of the element with the extra qualifiers on it. 2147c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // This can recursively sink qualifiers through multiple levels of arrays. 2148c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); 2149c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner NewEltTy = getCanonicalType(NewEltTy); 2150c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2151c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 215250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe( 215350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor getConstantArrayType(NewEltTy, CAT->getSize(), 215450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor CAT->getSizeModifier(), 215550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor CAT->getIndexTypeQualifier())); 2156c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) 215750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe( 215850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), 215950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor IAT->getIndexTypeQualifier())); 2160c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2161898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) 216250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe( 216350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor getDependentSizedArrayType(NewEltTy, 2164bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr() ? 2165bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr()->Retain() : 0, 216650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor DSAT->getSizeModifier(), 216750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor DSAT->getIndexTypeQualifier(), 216850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor DSAT->getBracketsRange())); 2169898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 2170c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner VariableArrayType *VAT = cast<VariableArrayType>(AT); 217150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, 2172bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr() ? 2173bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr()->Retain() : 0, 217450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor VAT->getSizeModifier(), 217550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor VAT->getIndexTypeQualifier(), 217650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor VAT->getBracketsRange())); 2177c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner} 2178c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 217925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { 218025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor // If this template name refers to a template, the canonical 218125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor // template name merely stores the template itself. 218225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor if (TemplateDecl *Template = Name.getAsTemplateDecl()) 218397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); 218425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor 2185d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor // If this template name refers to a set of overloaded function templates, 2186d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor /// the canonical template name merely stores the set of function templates. 21876ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) { 21886ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor OverloadedFunctionDecl *CanonOvl = 0; 21896ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), 21906ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor FEnd = Ovl->function_end(); 21916ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor F != FEnd; ++F) { 21926ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor Decl *Canon = F->get()->getCanonicalDecl(); 21936ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor if (CanonOvl || Canon != F->get()) { 21946ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor if (!CanonOvl) 21956ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor CanonOvl = OverloadedFunctionDecl::Create(*this, 21966ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor Ovl->getDeclContext(), 21976ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor Ovl->getDeclName()); 21986ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor 21996ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor CanonOvl->addOverload( 22006ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon))); 22016ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor } 22026ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor } 22036ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor 22046ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor return TemplateName(CanonOvl? CanonOvl : Ovl); 22056ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor } 2206d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 220725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor DependentTemplateName *DTN = Name.getAsDependentTemplateName(); 220825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor assert(DTN && "Non-dependent template names must refer to template decls."); 220925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor return DTN->CanonicalTemplateName; 221025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor} 221125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor 22121275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorTemplateArgument 22131275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { 22141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor switch (Arg.getKind()) { 22151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Null: 22161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return Arg; 22171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Expression: 22191275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // FIXME: Build canonical expression? 22201275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return Arg; 22211275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22221275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Declaration: 22231275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(SourceLocation(), 22241275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor Arg.getAsDecl()->getCanonicalDecl()); 22251275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22261275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Integral: 22271275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(SourceLocation(), 22281275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor *Arg.getAsIntegral(), 22291275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor getCanonicalType(Arg.getIntegralType())); 22301275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22311275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Type: 22321275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(SourceLocation(), 22331275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor getCanonicalType(Arg.getAsType())); 22341275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22351275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Pack: { 22361275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // FIXME: Allocate in ASTContext 22371275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; 22381275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor unsigned Idx = 0; 22391275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor for (TemplateArgument::pack_iterator A = Arg.pack_begin(), 22401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor AEnd = Arg.pack_end(); 22411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor A != AEnd; (void)++A, ++Idx) 22421275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs[Idx] = getCanonicalTemplateArgument(*A); 22431275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateArgument Result; 22451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); 22461275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return Result; 22471275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor } 22481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor } 22491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22501275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Silence GCC warning 22511275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor assert(false && "Unhandled template argument kind"); 22521275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(); 22531275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor} 22541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 2255d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier * 2256d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { 2257d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (!NNS) 2258d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return 0; 2259d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2260d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor switch (NNS->getKind()) { 2261d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::Identifier: 2262d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // Canonicalize the prefix but keep the identifier the same. 2263d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return NestedNameSpecifier::Create(*this, 2264d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor getCanonicalNestedNameSpecifier(NNS->getPrefix()), 2265d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor NNS->getAsIdentifier()); 2266d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2267d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::Namespace: 2268d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // A namespace is canonical; build a nested-name-specifier with 2269d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // this namespace and no prefix. 2270d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); 2271d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2272d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::TypeSpec: 2273d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::TypeSpecWithTemplate: { 2274d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); 2275495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor return NestedNameSpecifier::Create(*this, 0, 2276d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, 2277d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor T.getTypePtr()); 2278d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 2279d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2280d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::Global: 2281d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // The global specifier is canonical and unique. 2282d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return NNS; 2283d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 2284d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2285d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // Required to silence a GCC warning 2286d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return 0; 2287d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor} 2288d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2289c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2290c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) { 2291c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Handle the non-qualified case efficiently. 2292c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (T.getCVRQualifiers() == 0) { 2293c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Handle the common positive case fast. 2294c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const ArrayType *AT = dyn_cast<ArrayType>(T)) 2295c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return AT; 2296c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } 2297c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2298c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Handle the common negative case fast, ignoring CVR qualifiers. 2299c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType CType = T->getCanonicalTypeInternal(); 2300c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2301f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian // Make sure to look through type qualifiers (like ExtQuals) for the negative 2302c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // test. 2303c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (!isa<ArrayType>(CType) && 2304c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner !isa<ArrayType>(CType.getUnqualifiedType())) 2305c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return 0; 2306c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2307c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Apply any CVR qualifiers from the array type to the element type. This 2308c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // implements C99 6.7.3p8: "If the specification of an array type includes 2309c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // any type qualifiers, the element type is so qualified, not the array type." 2310c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2311c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If we get here, we either have type qualifiers on the type, or we have 2312c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // sugar such as a typedef in the way. If we have type qualifiers on the type 231350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor // we must propagate them down into the element type. 2314c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner unsigned CVRQuals = T.getCVRQualifiers(); 2315c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner unsigned AddrSpace = 0; 2316c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner Type *Ty = T.getTypePtr(); 2317c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2318f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian // Rip through ExtQualType's and typedefs to get to a concrete type. 2319c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner while (1) { 2320f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) { 2321f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian AddrSpace = EXTQT->getAddressSpace(); 2322f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian Ty = EXTQT->getBaseType(); 2323c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } else { 2324c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner T = Ty->getDesugaredType(); 2325c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) 2326c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner break; 2327c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner CVRQuals |= T.getCVRQualifiers(); 2328c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner Ty = T.getTypePtr(); 2329c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } 2330c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } 2331c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2332c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If we have a simple case, just return now. 2333c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner const ArrayType *ATy = dyn_cast<ArrayType>(Ty); 2334c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) 2335c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return ATy; 2336c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2337c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Otherwise, we have an array and we have qualifiers on it. Push the 2338c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // qualifiers into the array element type and return a new array type. 2339c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Get the canonical version of the element with the extra qualifiers on it. 2340c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // This can recursively sink qualifiers through multiple levels of arrays. 2341c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType NewEltTy = ATy->getElementType(); 2342c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (AddrSpace) 2343f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace); 2344c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); 2345c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2346c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) 2347c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), 2348c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner CAT->getSizeModifier(), 2349c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner CAT->getIndexTypeQualifier())); 2350c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) 2351c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return cast<ArrayType>(getIncompleteArrayType(NewEltTy, 2352c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner IAT->getSizeModifier(), 23537e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor IAT->getIndexTypeQualifier())); 2354898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 2355898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (const DependentSizedArrayType *DSAT 2356898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor = dyn_cast<DependentSizedArrayType>(ATy)) 2357898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return cast<ArrayType>( 2358898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor getDependentSizedArrayType(NewEltTy, 2359bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr() ? 2360bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr()->Retain() : 0, 2361898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor DSAT->getSizeModifier(), 23627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor DSAT->getIndexTypeQualifier(), 23637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor DSAT->getBracketsRange())); 2364c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2365c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner const VariableArrayType *VAT = cast<VariableArrayType>(ATy); 23667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return cast<ArrayType>(getVariableArrayType(NewEltTy, 2367bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr() ? 2368bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr()->Retain() : 0, 2369c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner VAT->getSizeModifier(), 23707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor VAT->getIndexTypeQualifier(), 23717e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor VAT->getBracketsRange())); 237277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner} 237377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner 237477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner 2375e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the 2376e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer. This operation is non-trivial when 2377e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc. The canonical type of "T" must be an array type, 2378e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array. 2379e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// 2380e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 2381e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) { 2382c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Get the element type with 'getAsArrayType' so that we don't lose any 2383c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // typedefs in the element type of the array. This also handles propagation 2384c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // of type qualifiers from the array type into the element type if present 2385c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // (C99 6.7.3p8). 2386c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner const ArrayType *PrettyArrayType = getAsArrayType(Ty); 2387c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner assert(PrettyArrayType && "Not an array type!"); 2388e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 2389c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); 2390e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 2391e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner // int x[restrict 4] -> int *restrict 2392c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); 2393e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner} 2394e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 23955e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) { 23965e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor QualifierSet qualifiers; 23975e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor while (true) { 23985e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor const Type *UT = qualifiers.strip(QT); 23995e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { 24005e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor QT = AT->getElementType(); 24016dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump } else { 24025e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor return qualifiers.apply(QT, *this); 24035e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor } 24045e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor } 24055e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor} 24065e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor 2407d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel DunbarQualType ASTContext::getBaseElementType(const VariableArrayType *VAT) { 24086183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson QualType ElemTy = VAT->getElementType(); 24096183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson 24106183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy)) 24116183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson return getBaseElementType(VAT); 24126183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson 24136183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson return ElemTy; 24146183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson} 24156183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson 24160de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements. 24170de78998e7bda473b408437053e48661b510d453Fariborz Jahanianuint64_t 24180de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { 24190de78998e7bda473b408437053e48661b510d453Fariborz Jahanian uint64_t ElementCount = 1; 24200de78998e7bda473b408437053e48661b510d453Fariborz Jahanian do { 24210de78998e7bda473b408437053e48661b510d453Fariborz Jahanian ElementCount *= CA->getSize().getZExtValue(); 24220de78998e7bda473b408437053e48661b510d453Fariborz Jahanian CA = dyn_cast<ConstantArrayType>(CA->getElementType()); 24230de78998e7bda473b408437053e48661b510d453Fariborz Jahanian } while (CA); 24240de78998e7bda473b408437053e48661b510d453Fariborz Jahanian return ElementCount; 24250de78998e7bda473b408437053e48661b510d453Fariborz Jahanian} 24260de78998e7bda473b408437053e48661b510d453Fariborz Jahanian 24275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types. 24285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float. 2429a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) { 2430ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb if (const ComplexType *CT = T->getAsComplexType()) 24315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getFloatingRank(CT->getElementType()); 2432a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner 2433d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel Dunbar assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type"); 2434ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb switch (T->getAsBuiltinType()->getKind()) { 2435a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner default: assert(0 && "getFloatingRank(): not a floating type"); 24365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Float: return FloatRank; 24375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Double: return DoubleRank; 24385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::LongDouble: return LongDoubleRank; 24395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2442716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// getFloatingTypeOfSizeWithinDomain - Returns a real floating 2443716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// point or a complex type (based on typeDomain/typeSize). 2444716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type. 2445716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type. 24461361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, 24471361b11066239ea15764a2a844405352d87296b3Chris Lattner QualType Domain) const { 24481361b11066239ea15764a2a844405352d87296b3Chris Lattner FloatingRank EltRank = getFloatingRank(Size); 24491361b11066239ea15764a2a844405352d87296b3Chris Lattner if (Domain->isComplexType()) { 24501361b11066239ea15764a2a844405352d87296b3Chris Lattner switch (EltRank) { 2451716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff default: assert(0 && "getFloatingRank(): illegal value for rank"); 2452f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff case FloatRank: return FloatComplexTy; 2453f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff case DoubleRank: return DoubleComplexTy; 2454f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff case LongDoubleRank: return LongDoubleComplexTy; 2455f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff } 2456f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff } 24571361b11066239ea15764a2a844405352d87296b3Chris Lattner 24581361b11066239ea15764a2a844405352d87296b3Chris Lattner assert(Domain->isRealFloatingType() && "Unknown domain!"); 24591361b11066239ea15764a2a844405352d87296b3Chris Lattner switch (EltRank) { 24601361b11066239ea15764a2a844405352d87296b3Chris Lattner default: assert(0 && "getFloatingRank(): illegal value for rank"); 24611361b11066239ea15764a2a844405352d87296b3Chris Lattner case FloatRank: return FloatTy; 24621361b11066239ea15764a2a844405352d87296b3Chris Lattner case DoubleRank: return DoubleTy; 24631361b11066239ea15764a2a844405352d87296b3Chris Lattner case LongDoubleRank: return LongDoubleTy; 24645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 24677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating 24687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' == 24697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 24707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1. 2471a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { 2472a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner FloatingRank LHSR = getFloatingRank(LHS); 2473a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner FloatingRank RHSR = getFloatingRank(RHS); 2474a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner 2475a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner if (LHSR == RHSR) 2476fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff return 0; 2477a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner if (LHSR > RHSR) 2478fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff return 1; 2479fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff return -1; 24805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2482f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This 2483f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum, 2484f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized. 2485f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) { 2486f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner assert(T->isCanonical() && "T should be canonicalized"); 2487f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman if (EnumType* ET = dyn_cast<EnumType>(T)) 2488f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman T = ET->getDecl()->getIntegerType().getTypePtr(); 2489f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman 2490a34267595534a72703290153a6f7e3da1adcec59Eli Friedman if (T->isSpecificBuiltinType(BuiltinType::WChar)) 2491a34267595534a72703290153a6f7e3da1adcec59Eli Friedman T = getFromTargetType(Target.getWCharType()).getTypePtr(); 2492a34267595534a72703290153a6f7e3da1adcec59Eli Friedman 2493f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith if (T->isSpecificBuiltinType(BuiltinType::Char16)) 2494f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith T = getFromTargetType(Target.getChar16Type()).getTypePtr(); 2495f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith 2496f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith if (T->isSpecificBuiltinType(BuiltinType::Char32)) 2497f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith T = getFromTargetType(Target.getChar32Type()).getTypePtr(); 2498f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith 2499f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // There are two things which impact the integer rank: the width, and 2500f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // the ordering of builtins. The builtin ordering is encoded in the 2501f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // bottom three bits; the width is encoded in the bits above that. 25021b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) 2503f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return FWIT->getWidth() << 3; 2504f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman 2505f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner switch (cast<BuiltinType>(T)->getKind()) { 25067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner default: assert(0 && "getIntegerRank(): not a built-in integer"); 25077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Bool: 2508f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 1 + (getIntWidth(BoolTy) << 3); 25097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Char_S: 25107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Char_U: 25117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::SChar: 25127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::UChar: 2513f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 2 + (getIntWidth(CharTy) << 3); 25147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Short: 25157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::UShort: 2516f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 3 + (getIntWidth(ShortTy) << 3); 25177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Int: 25187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::UInt: 2519f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 4 + (getIntWidth(IntTy) << 3); 25207cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Long: 25217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::ULong: 2522f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 5 + (getIntWidth(LongTy) << 3); 25237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::LongLong: 25247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::ULongLong: 2525f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 6 + (getIntWidth(LongLongTy) << 3); 25262df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::Int128: 25272df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::UInt128: 25282df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner return 7 + (getIntWidth(Int128Ty) << 3); 2529f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner } 2530f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner} 2531f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner 253204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according 253304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 253404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// 253504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no 253604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs. 253704e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) { 253804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman FieldDecl *Field = E->getBitField(); 253904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman if (!Field) 254004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return QualType(); 254104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 254204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman QualType FT = Field->getType(); 254304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 254404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); 254504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman uint64_t BitWidth = BitWidthAP.getZExtValue(); 254604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman uint64_t IntSize = getTypeSize(IntTy); 254704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // GCC extension compatibility: if the bit-field size is less than or equal 254804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // to the size of int, it gets promoted no matter what its type is. 254904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // For instance, unsigned long bf : 4 gets promoted to signed int. 255004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman if (BitWidth < IntSize) 255104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return IntTy; 255204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 255304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman if (BitWidth == IntSize) 255404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; 255504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 255604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // Types bigger than int are not subject to promotions, and therefore act 255704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // like the base type. 255804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // FIXME: This doesn't quite match what gcc does, but what gcc does here 255904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // is ridiculous. 256004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return QualType(); 256104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman} 256204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 2563a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will 2564a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 2565a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type. 2566a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) { 2567a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(!Promotable.isNull()); 2568a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(Promotable->isPromotableIntegerType()); 2569a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (Promotable->isSignedIntegerType()) 2570a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return IntTy; 2571a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman uint64_t PromotableSize = getTypeSize(Promotable); 2572a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman uint64_t IntSize = getTypeSize(IntTy); 2573a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); 2574a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; 2575a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman} 2576a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 25777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getIntegerTypeOrder - Returns the highest ranked integer type: 25787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 25797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1. 25807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { 2581f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Type *LHSC = getCanonicalType(LHS).getTypePtr(); 2582f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Type *RHSC = getCanonicalType(RHS).getTypePtr(); 25837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSC == RHSC) return 0; 25845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2585f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner bool LHSUnsigned = LHSC->isUnsignedIntegerType(); 2586f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner bool RHSUnsigned = RHSC->isUnsignedIntegerType(); 25875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner unsigned LHSRank = getIntegerRank(LHSC); 25897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner unsigned RHSRank = getIntegerRank(RHSC); 25905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. 25927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSRank == RHSRank) return 0; 25937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return LHSRank > RHSRank ? 1 : -1; 25947cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner } 25957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner 25967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. 25977cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSUnsigned) { 25987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the unsigned [LHS] type is larger, return it. 25997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSRank >= RHSRank) 26007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return 1; 26017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner 26027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the signed type can represent all values of the unsigned type, it 26037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // wins. Because we are dealing with 2's complement and types that are 26047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // powers of two larger than each other, this is always safe. 26057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return -1; 26067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner } 26077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner 26087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the unsigned [RHS] type is larger, return it. 26097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (RHSRank >= LHSRank) 26107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return -1; 26115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 26127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the signed type can represent all values of the unsigned type, it 26137cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // wins. Because we are dealing with 2's complement and types that are 26147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // powers of two larger than each other, this is always safe. 26157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return 1; 26165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 261771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 261871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson// getCFConstantStringType - Return the type used for constant CFStrings. 261971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() { 262071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson if (!CFConstantStringTypeDecl) { 26216c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner CFConstantStringTypeDecl = 262239ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2623df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek &Idents.get("NSConstantString")); 2624f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson QualType FieldTypes[4]; 262571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 262671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson // const int *isa; 262771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); 2628f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson // int flags; 2629f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson FieldTypes[1] = IntTy; 263071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson // const char *str; 2631f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); 263271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson // long length; 2633f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson FieldTypes[3] = LongTy; 263471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 263544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor // Create fields 263644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor for (unsigned i = 0; i < 4; ++i) { 263744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, 263844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor SourceLocation(), 0, 2639a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis FieldTypes[i], /*DInfo=*/0, 2640a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis /*BitWidth=*/0, 26414afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor /*Mutable=*/false); 264217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis CFConstantStringTypeDecl->addDecl(Field); 264344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor } 264444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 264544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringTypeDecl->completeDefinition(*this); 264671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson } 264771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 264871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson return getTagDeclType(CFConstantStringTypeDecl); 26498467583c2704e7a9691ea56939a029015f0ade0aGabor Greif} 2650b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson 2651319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) { 26526217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RecordType *Rec = T->getAs<RecordType>(); 2653319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor assert(Rec && "Invalid CFConstantStringType"); 2654319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor CFConstantStringTypeDecl = Rec->getDecl(); 2655319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor} 2656319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor 2657bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders CarlssonQualType ASTContext::getObjCFastEnumerationStateType() 2658bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson{ 2659bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson if (!ObjCFastEnumerationStateTypeDecl) { 266044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor ObjCFastEnumerationStateTypeDecl = 266144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 266244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor &Idents.get("__objcFastEnumerationState")); 266344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 2664bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson QualType FieldTypes[] = { 2665bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson UnsignedLongTy, 2666de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff getPointerType(ObjCIdTypedefType), 2667bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson getPointerType(UnsignedLongTy), 2668bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson getConstantArrayType(UnsignedLongTy, 2669bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson llvm::APInt(32, 5), ArrayType::Normal, 0) 2670bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson }; 2671bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 267244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor for (size_t i = 0; i < 4; ++i) { 267344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *Field = FieldDecl::Create(*this, 267444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor ObjCFastEnumerationStateTypeDecl, 267544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor SourceLocation(), 0, 2676a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis FieldTypes[i], /*DInfo=*/0, 2677a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis /*BitWidth=*/0, 26784afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor /*Mutable=*/false); 267917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ObjCFastEnumerationStateTypeDecl->addDecl(Field); 268044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor } 2681bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 268244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); 2683bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson } 2684bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 2685bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 2686bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson} 2687bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 2688319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) { 26896217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RecordType *Rec = T->getAs<RecordType>(); 2690319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor assert(Rec && "Invalid ObjCFAstEnumerationStateType"); 2691319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); 2692319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor} 2693319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor 2694e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL: 2695e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL; 26962d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) { 2697e8c49533521c40643653f943d47229e62d277f88Anders Carlsson if (const TypedefType *TT = dyn_cast<TypedefType>(T)) 2698bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) 2699bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner return II->isStr("BOOL"); 270085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 270185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson return false; 270285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson} 270385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 2704a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding 270533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose. 2706a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) { 270798be4943e8dc4f3905629a7102668960873cf863Chris Lattner uint64_t sz = getTypeSize(type); 270833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 270933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Make all integer and enum types at least as large as an int 271033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian if (sz > 0 && type->isIntegralType()) 271198be4943e8dc4f3905629a7102668960873cf863Chris Lattner sz = std::max(sz, getTypeSize(IntTy)); 271233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Treat arrays as pointers, since that's how they're passed in. 271333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian else if (type->isArrayType()) 271498be4943e8dc4f3905629a7102668960873cf863Chris Lattner sz = getTypeSize(VoidPtrTy); 271598be4943e8dc4f3905629a7102668960873cf863Chris Lattner return sz / getTypeSize(CharTy); 271633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian} 271733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 2718a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method 271933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration. 2720c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, 2721e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner std::string& S) { 2722c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: This is not very efficient. 2723ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian // Encode type qualifer, 'in', 'inout', etc. for the return type. 2724a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); 272533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Encode result type. 27260d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar getObjCEncodingForType(Decl->getResultType(), S); 272733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Compute size of all parameters. 272833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Start with computing size of a pointer in number of bytes. 272933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // FIXME: There might(should) be a better way of doing this computation! 273033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian SourceLocation Loc; 273198be4943e8dc4f3905629a7102668960873cf863Chris Lattner int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); 273233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // The first two arguments (self and _cmd) are pointers; account for 273333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // their size. 273433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian int ParmOffset = 2 * PtrSize; 273589951a86b594513c2a013532ed45d197413b1087Chris Lattner for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 273689951a86b594513c2a013532ed45d197413b1087Chris Lattner E = Decl->param_end(); PI != E; ++PI) { 273789951a86b594513c2a013532ed45d197413b1087Chris Lattner QualType PType = (*PI)->getType(); 273889951a86b594513c2a013532ed45d197413b1087Chris Lattner int sz = getObjCEncodingTypeSize(PType); 2739a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); 274033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian ParmOffset += sz; 274133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian } 274233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += llvm::utostr(ParmOffset); 274333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += "@0:"; 274433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += llvm::utostr(PtrSize); 274533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 274633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Argument types. 274733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian ParmOffset = 2 * PtrSize; 274889951a86b594513c2a013532ed45d197413b1087Chris Lattner for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 274989951a86b594513c2a013532ed45d197413b1087Chris Lattner E = Decl->param_end(); PI != E; ++PI) { 275089951a86b594513c2a013532ed45d197413b1087Chris Lattner ParmVarDecl *PVDecl = *PI; 27514306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian QualType PType = PVDecl->getOriginalType(); 27524306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian if (const ArrayType *AT = 2753ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { 2754ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff // Use array's original type only if it has known number of 2755ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff // elements. 2756bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff if (!isa<ConstantArrayType>(AT)) 2757ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff PType = PVDecl->getType(); 2758ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff } else if (PType->isFunctionType()) 2759ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff PType = PVDecl->getType(); 2760ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian // Process argument qualifiers for user supplied arguments; such as, 276133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // 'in', 'inout', etc. 27624306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); 27630d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar getObjCEncodingForType(PType, S); 276433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += llvm::utostr(ParmOffset); 2765a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ParmOffset += getObjCEncodingTypeSize(PType); 276633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian } 276733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian} 276833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 2769c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this 277083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an 2771c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be 2772c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties. 277383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// Property attributes are stored as a comma-delimited C string. The simple 277483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// attributes readonly and bycopy are encoded as single characters. The 277583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// parametrized attributes, getter=name, setter=name, and ivar=name, are 277683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// encoded as single characters, followed by an identifier. Property types 277783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// are also encoded as a parametrized attribute. The characters used to encode 277883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration: 277983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code 278083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes { 278183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R', // property is read-only. 278283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C', // property is a copy of the value last assigned 278383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&', // property is a reference to the value last assigned 278483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D', // property is dynamic 278583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G', // followed by getter selector name 278683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S', // followed by setter selector name 278783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V' // followed by instance variable name 278883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't' // followed by old-style type encoding. 278983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W' // 'weak' property 279083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P' // property GC'able 279183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N' // property non-atomic 279283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// }; 279383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode 2794c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 2795c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar const Decl *Container, 2796e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner std::string& S) { 2797c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // Collect information from the property implementation decl(s). 2798c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar bool Dynamic = false; 2799c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar ObjCPropertyImplDecl *SynthesizePID = 0; 2800c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2801c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: Duplicated code due to poor abstraction. 2802c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (Container) { 2803c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (const ObjCCategoryImplDecl *CID = 2804c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar dyn_cast<ObjCCategoryImplDecl>(Container)) { 2805c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar for (ObjCCategoryImplDecl::propimpl_iterator 280617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = CID->propimpl_begin(), e = CID->propimpl_end(); 2807653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor i != e; ++i) { 2808c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 2809c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyDecl() == PD) { 2810c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 2811c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar Dynamic = true; 2812c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 2813c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar SynthesizePID = PID; 2814c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2815c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2816c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2817c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 281861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); 2819c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar for (ObjCCategoryImplDecl::propimpl_iterator 282017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = OID->propimpl_begin(), e = OID->propimpl_end(); 2821653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor i != e; ++i) { 2822c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 2823c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyDecl() == PD) { 2824c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 2825c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar Dynamic = true; 2826c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 2827c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar SynthesizePID = PID; 2828c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2829c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2830c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2831c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2832c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2833c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2834c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: This is not very efficient. 2835c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S = "T"; 2836c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2837c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // Encode result type. 2838090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian // GCC has some special rules regarding encoding of properties which 2839090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian // closely resembles encoding of ivars. 2840153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, 2841090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian true /* outermost type */, 2842090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian true /* encoding for property */); 2843c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2844c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PD->isReadOnly()) { 2845c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",R"; 2846c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 2847c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar switch (PD->getSetterKind()) { 2848c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar case ObjCPropertyDecl::Assign: break; 2849c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar case ObjCPropertyDecl::Copy: S += ",C"; break; 2850c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar case ObjCPropertyDecl::Retain: S += ",&"; break; 2851c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2852c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2853c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2854c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // It really isn't clear at all what this means, since properties 2855c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // are "dynamic by default". 2856c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (Dynamic) 2857c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",D"; 2858c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2859090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) 2860090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian S += ",N"; 2861090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian 2862c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 2863c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",G"; 2864077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner S += PD->getGetterName().getAsString(); 2865c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2866c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2867c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 2868c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",S"; 2869077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner S += PD->getSetterName().getAsString(); 2870c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2871c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2872c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (SynthesizePID) { 2873c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); 2874c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",V"; 287539f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner S += OID->getNameAsString(); 2876c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2877c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2878c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: OBJCGC: weak & strong 2879c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar} 2880c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2881a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding - 2882a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// Another legacy compatibility encoding: 32-bit longs are encoded as 2883c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian/// 'l' or 'L' , but not always. For typedefs, we need to use 2884a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer! 2885a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 2886a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { 28878e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump if (isa<TypedefType>(PointeeTy.getTypePtr())) { 2888a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) { 2889c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian if (BT->getKind() == BuiltinType::ULong && 2890c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 2891a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian PointeeTy = UnsignedIntTy; 2892c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian else 2893c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian if (BT->getKind() == BuiltinType::Long && 2894c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 2895a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian PointeeTy = IntTy; 2896a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2897a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2898a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian} 2899a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian 29007d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S, 2901153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar const FieldDecl *Field) { 290282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // We follow the behavior of gcc, expanding structures which are 290382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // directly pointed to, and expanding embedded structures. Note that 290482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // these rules are sufficient to prevent recursive encoding of the 290582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // same type. 29065b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian getObjCEncodingForTypeImpl(T, S, true, true, Field, 29075b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian true /* outermost type */); 290882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar} 290982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar 29108b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanianstatic void EncodeBitField(const ASTContext *Context, std::string& S, 2911153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar const FieldDecl *FD) { 29128b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian const Expr *E = FD->getBitWidth(); 29138b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); 29148b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian ASTContext *Ctx = const_cast<ASTContext*>(Context); 29159a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); 29168b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian S += 'b'; 29178b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian S += llvm::utostr(N); 29188b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian} 29198b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian 292082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, 292182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar bool ExpandPointedToStructures, 292282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar bool ExpandStructures, 2923153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar const FieldDecl *FD, 2924090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian bool OutermostType, 29256ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor bool EncodingProperty) { 2926e8c49533521c40643653f943d47229e62d277f88Anders Carlsson if (const BuiltinType *BT = T->getAsBuiltinType()) { 2927ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (FD && FD->isBitField()) 2928ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return EncodeBitField(this, S, FD); 2929ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner char encoding; 2930ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner switch (BT->getKind()) { 2931ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner default: assert(0 && "Unhandled builtin type kind"); 2932ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Void: encoding = 'v'; break; 2933ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Bool: encoding = 'B'; break; 2934ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Char_U: 2935ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UChar: encoding = 'C'; break; 2936ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UShort: encoding = 'S'; break; 2937ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UInt: encoding = 'I'; break; 2938ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::ULong: 293972696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian encoding = 2940ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; 294172696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian break; 2942ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UInt128: encoding = 'T'; break; 2943ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::ULongLong: encoding = 'Q'; break; 2944ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Char_S: 2945ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::SChar: encoding = 'c'; break; 2946ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Short: encoding = 's'; break; 2947ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Int: encoding = 'i'; break; 2948ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Long: 2949ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner encoding = 2950ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; 2951ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner break; 2952ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::LongLong: encoding = 'q'; break; 2953ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Int128: encoding = 't'; break; 2954ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Float: encoding = 'f'; break; 2955ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Double: encoding = 'd'; break; 2956ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::LongDouble: encoding = 'd'; break; 295743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian } 2958ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 2959ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += encoding; 2960ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 2961ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 2962ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 2963ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (const ComplexType *CT = T->getAsComplexType()) { 2964c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson S += 'j'; 2965c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, 2966c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson false); 2967ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 2968ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 2969ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 29706217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const PointerType *PT = T->getAs<PointerType>()) { 297185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson QualType PointeeTy = PT->getPointeeType(); 2972a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian bool isReadOnly = false; 2973a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // For historical/compatibility reasons, the read-only qualifier of the 2974a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // pointee gets emitted _before_ the '^'. The read-only qualifier of 2975a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // the pointer itself gets ignored, _unless_ we are looking at a typedef! 2976a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // Also, do not emit the 'r' for anything but the outermost type! 29778e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump if (isa<TypedefType>(T.getTypePtr())) { 2978a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (OutermostType && T.isConstQualified()) { 2979a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian isReadOnly = true; 2980a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian S += 'r'; 2981a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 29829fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else if (OutermostType) { 2983a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian QualType P = PointeeTy; 29846217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek while (P->getAs<PointerType>()) 29856217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek P = P->getAs<PointerType>()->getPointeeType(); 2986a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (P.isConstQualified()) { 2987a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian isReadOnly = true; 2988a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian S += 'r'; 2989a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2990a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2991a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (isReadOnly) { 2992a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // Another legacy compatibility encoding. Some ObjC qualifier and type 2993a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // combinations need to be rearranged. 2994a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // Rewrite "in const" from "nr" to "rn" 2995a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian const char * s = S.c_str(); 2996a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian int len = S.length(); 2997a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { 2998a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian std::string replace = "rn"; 2999a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian S.replace(S.end()-2, S.end(), replace); 3000a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 3001a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 300214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (isObjCSelType(PointeeTy)) { 30038baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson S += ':'; 30048baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson return; 3005c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian } 300685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 300785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson if (PointeeTy->isCharType()) { 300885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson // char pointer types should be encoded as '*' unless it is a 300985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson // type that has been typedef'd to 'BOOL'. 3010e8c49533521c40643653f943d47229e62d277f88Anders Carlsson if (!isTypeTypedefedAsBOOL(PointeeTy)) { 301185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson S += '*'; 301285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson return; 301385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson } 30146217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { 30159533a7fdb8397421f3be52e879442460a87389f6Steve Naroff // GCC binary compat: Need to convert "struct objc_class *" to "#". 30169533a7fdb8397421f3be52e879442460a87389f6Steve Naroff if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { 30179533a7fdb8397421f3be52e879442460a87389f6Steve Naroff S += '#'; 30189533a7fdb8397421f3be52e879442460a87389f6Steve Naroff return; 30199533a7fdb8397421f3be52e879442460a87389f6Steve Naroff } 30209533a7fdb8397421f3be52e879442460a87389f6Steve Naroff // GCC binary compat: Need to convert "struct objc_object *" to "@". 30219533a7fdb8397421f3be52e879442460a87389f6Steve Naroff if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { 30229533a7fdb8397421f3be52e879442460a87389f6Steve Naroff S += '@'; 30239533a7fdb8397421f3be52e879442460a87389f6Steve Naroff return; 30249533a7fdb8397421f3be52e879442460a87389f6Steve Naroff } 30259533a7fdb8397421f3be52e879442460a87389f6Steve Naroff // fall through... 302685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson } 302785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson S += '^'; 3028a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian getLegacyIntegralTypeEncoding(PointeeTy); 3029a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian 3030ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, 303143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian NULL); 3032ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3033ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3034ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3035ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (const ArrayType *AT = 3036ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // Ignore type qualifiers etc. 3037ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { 3038559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson if (isa<IncompleteArrayType>(AT)) { 3039559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson // Incomplete arrays are encoded as a pointer to the array element. 3040559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += '^'; 3041559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson 3042559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson getObjCEncodingForTypeImpl(AT->getElementType(), S, 3043559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson false, ExpandStructures, FD); 3044559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson } else { 3045559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += '['; 304685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 3047559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 3048559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += llvm::utostr(CAT->getSize().getZExtValue()); 3049559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson else { 3050559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson //Variable length arrays are encoded as a regular array with 0 elements. 3051559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson assert(isa<VariableArrayType>(AT) && "Unknown array type!"); 3052559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += '0'; 3053559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson } 305485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 3055559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson getObjCEncodingForTypeImpl(AT->getElementType(), S, 3056559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson false, ExpandStructures, FD); 3057559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += ']'; 3058559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson } 3059ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3060ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3061ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3062ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->getAsFunctionType()) { 3063c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson S += '?'; 3064ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3065ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3066ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 30676217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const RecordType *RTy = T->getAs<RecordType>()) { 306882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar RecordDecl *RDecl = RTy->getDecl(); 3069d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += RDecl->isUnion() ? '(' : '{'; 3070502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar // Anonymous structures print as '?' 3071502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar if (const IdentifierInfo *II = RDecl->getIdentifier()) { 3072502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar S += II->getName(); 3073502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar } else { 3074502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar S += '?'; 3075502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar } 30760d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar if (ExpandStructures) { 30777d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian S += '='; 307817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::field_iterator Field = RDecl->field_begin(), 307917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis FieldEnd = RDecl->field_end(); 308044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor Field != FieldEnd; ++Field) { 308143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian if (FD) { 3082d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += '"'; 308344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor S += Field->getNameAsString(); 3084d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += '"'; 3085d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar } 3086d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar 3087d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar // Special case bit-fields. 308843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian if (Field->isBitField()) { 308943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian getObjCEncodingForTypeImpl(Field->getType(), S, false, true, 309043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian (*Field)); 3091d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar } else { 3092a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian QualType qt = Field->getType(); 3093a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian getLegacyIntegralTypeEncoding(qt); 3094a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian getObjCEncodingForTypeImpl(qt, S, false, true, 309543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian FD); 3096d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar } 30977d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian } 30986de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian } 3099d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += RDecl->isUnion() ? ')' : '}'; 3100ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3101ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3102ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3103ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->isEnumeralType()) { 31048b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian if (FD && FD->isBitField()) 31058b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian EncodeBitField(this, S, FD); 31068b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian else 31078b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian S += 'i'; 3108ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3109ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3110ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3111ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->isBlockPointerType()) { 311221a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff S += "@?"; // Unlike a pointer-to-function, which is "^?". 3113ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3114ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3115ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3116ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->isObjCInterfaceType()) { 311743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian // @encode(class_name) 311843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl(); 311943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += '{'; 312043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian const IdentifierInfo *II = OI->getIdentifier(); 312143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += II->getName(); 312243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += '='; 3123f1690858344968358131f8d5690d9ee458883000Chris Lattner llvm::SmallVector<FieldDecl*, 32> RecFields; 312443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian CollectObjCIvars(OI, RecFields); 3125f1690858344968358131f8d5690d9ee458883000Chris Lattner for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { 312643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian if (RecFields[i]->isBitField()) 312743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 312843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian RecFields[i]); 312943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian else 313043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 313143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian FD); 313243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian } 313343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += '}'; 3134ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 313543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian } 3136ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3137ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) { 313814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (OPT->isObjCIdType()) { 313914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '@'; 314014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 3141ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3142ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3143ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (OPT->isObjCClassType()) { 314414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '#'; 314514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 3146ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3147ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3148ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (OPT->isObjCQualifiedIdType()) { 314914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff getObjCEncodingForTypeImpl(getObjCIdType(), S, 315014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff ExpandPointedToStructures, 315114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff ExpandStructures, FD); 315214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (FD || EncodingProperty) { 315314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff // Note that we do extended encoding of protocol qualifer list 315414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff // Only when doing ivar or property encoding. 315514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '"'; 315667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 315767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff E = OPT->qual_end(); I != E; ++I) { 315814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '<'; 315914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += (*I)->getNameAsString(); 316014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '>'; 316114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 316214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '"'; 316314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 316414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 3165ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3166ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3167ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner QualType PointeeTy = OPT->getPointeeType(); 3168ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (!EncodingProperty && 3169ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner isa<TypedefType>(PointeeTy.getTypePtr())) { 3170ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // Another historical/compatibility reason. 3171ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // We encode the underlying type which comes out as 3172ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // {...}; 3173ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '^'; 3174ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner getObjCEncodingForTypeImpl(PointeeTy, S, 3175ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner false, ExpandPointedToStructures, 3176ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner NULL); 317714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 317814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 3179ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3180ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '@'; 3181ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (FD || EncodingProperty) { 3182ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '"'; 318367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff S += OPT->getInterfaceDecl()->getNameAsCString(); 318467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 318567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff E = OPT->qual_end(); I != E; ++I) { 3186ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '<'; 3187ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += (*I)->getNameAsString(); 3188ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '>'; 3189ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3190ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '"'; 3191ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3192ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3193ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3194ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3195ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner assert(0 && "@encode for type not implemented!"); 319685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson} 319785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 3198a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 3199ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian std::string& S) const { 3200ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_In) 3201ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'n'; 3202ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Inout) 3203ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'N'; 3204ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Out) 3205ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'o'; 3206ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Bycopy) 3207ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'O'; 3208ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Byref) 3209ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'R'; 3210ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Oneway) 3211ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'V'; 3212ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian} 3213ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian 3214ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) { 3215b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); 3216b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson 3217b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson BuiltinVaListType = T; 3218b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson} 3219b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson 3220ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) { 3221de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff ObjCIdTypedefType = T; 32227e219e47de26346885d667131977bd9ca2d7662aSteve Naroff} 32237e219e47de26346885d667131977bd9ca2d7662aSteve Naroff 3224ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) { 3225319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor ObjCSelType = T; 3226319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor 3227319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor const TypedefType *TT = T->getAsTypedefType(); 3228319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor if (!TT) 3229319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor return; 3230319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor TypedefDecl *TD = TT->getDecl(); 3231b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian 3232b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian // typedef struct objc_selector *SEL; 32336217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>(); 3234c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian if (!ptr) 3235c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian return; 3236b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 3237c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian if (!rec) 3238c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian return; 3239b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian SelStructType = rec; 3240b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian} 3241b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian 3242ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) { 3243a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCProtoType = QT; 3244390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian} 3245390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian 3246ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) { 3247de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff ObjCClassTypedefType = T; 32488baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson} 32498baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson 3250a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { 3251a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek assert(ObjCConstantStringType.isNull() && 32522198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff "'NSConstantString' type already set!"); 32532198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff 3254a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCConstantStringType = getObjCInterfaceType(Decl); 32552198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff} 32562198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff 32577532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a qualified 32587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c std::vector. 32597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 32607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor bool TemplateKeyword, 32617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor TemplateDecl *Template) { 32627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor llvm::FoldingSetNodeID ID; 32637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); 32647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor void *InsertPos = 0; 32667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateName *QTN = 32677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 32687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor if (!QTN) { 32697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); 32707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateNames.InsertNode(QTN, InsertPos); 32717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor } 32727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor return TemplateName(QTN); 32747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor} 32757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 3276d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified 3277d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector. 3278d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 3279d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor bool TemplateKeyword, 3280d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor OverloadedFunctionDecl *Template) { 3281d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor llvm::FoldingSetNodeID ID; 3282d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); 3283d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 3284d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor void *InsertPos = 0; 3285d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateName *QTN = 3286d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 3287d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor if (!QTN) { 3288d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); 3289d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateNames.InsertNode(QTN, InsertPos); 3290d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor } 3291d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 3292d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor return TemplateName(QTN); 3293d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor} 3294d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 32957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent 32967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply. 32977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, 32987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor const IdentifierInfo *Name) { 32997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor assert(NNS->isDependent() && "Nested name specifier must be dependent"); 33007532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 33017532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor llvm::FoldingSetNodeID ID; 33027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateName::Profile(ID, NNS, Name); 33037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 33047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor void *InsertPos = 0; 33057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateName *QTN = 33067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 33077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 33087532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor if (QTN) 33097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor return TemplateName(QTN); 33107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 33117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 33127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor if (CanonNNS == NNS) { 33137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QTN = new (*this,4) DependentTemplateName(NNS, Name); 33147532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor } else { 33157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor TemplateName Canon = getDependentTemplateName(CanonNNS, Name); 33167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); 33177532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor } 33187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 33197532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateNames.InsertNode(QTN, InsertPos); 33207532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor return TemplateName(QTN); 33217532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor} 33227532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 3323b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by 3324d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type 3325d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType. 3326d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas GregorQualType ASTContext::getFromTargetType(unsigned Type) const { 3327b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor switch (Type) { 3328b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::NoInt: return QualType(); 3329b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedShort: return ShortTy; 3330b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedShort: return UnsignedShortTy; 3331b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedInt: return IntTy; 3332b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedInt: return UnsignedIntTy; 3333b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedLong: return LongTy; 3334b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedLong: return UnsignedLongTy; 3335b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedLongLong: return LongLongTy; 3336b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; 3337b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor } 3338b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor 3339b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor assert(false && "Unhandled TargetInfo::IntType value"); 3340b3ac5434ab936f092b8cc48349cb01db3a1e1c76Daniel Dunbar return QualType(); 3341b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor} 3342b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek 3343b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===// 3344b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek// Type Predicates. 3345b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===// 3346b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek 3347fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using 3348fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type. 3349fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types. 3350f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type. 3351fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// 3352fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const { 3353fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { 3354fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian if (TypedefDecl *TD = TDT->getDecl()) 335540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TD->getAttr<ObjCNSObjectAttr>()) 3356fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian return true; 3357fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian } 3358fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian return false; 3359fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian} 3360fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian 33614fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 33624fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute. 33634fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// 33644fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz JahanianQualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const { 3365b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner QualType::GCAttrTypes GCAttrs = QualType::GCNone; 33664fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian if (getLangOptions().ObjC1 && 33674fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian getLangOptions().getGCMode() != LangOptions::NonGC) { 3368b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner GCAttrs = Ty.getObjCGCAttr(); 33694fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian // Default behavious under objective-c's gc is for objective-c pointers 3370a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian // (or pointers to them) be treated as though they were declared 3371a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian // as __strong. 3372a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian if (GCAttrs == QualType::GCNone) { 3373f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff if (Ty->isObjCObjectPointerType()) 3374a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian GCAttrs = QualType::Strong; 3375a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian else if (Ty->isPointerType()) 33766217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); 3377a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian } 3378c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian // Non-pointers have none gc'able attribute regardless of the attribute 3379c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian // set on them. 3380f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) 3381c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian return QualType::GCNone; 33824fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian } 3383b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return GCAttrs; 33844fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian} 33854fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian 33866ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===// 33876ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner// Type Compatibility Testing 33886ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===// 3389770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner 33906ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// areCompatVectorTypes - Return true if the two specified vector types are 33916ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible. 33926ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS, 33936ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner const VectorType *RHS) { 33946ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner assert(LHS->isCanonical() && RHS->isCanonical()); 33956ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return LHS->getElementType() == RHS->getElementType() && 339661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner LHS->getNumElements() == RHS->getNumElements(); 33976ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner} 33986ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 33994084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===// 34004084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. 34014084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===// 34024084c306635b70f37029dca938444e6013f08684Steve Naroff 34034084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the 34044084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'. 34050fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 34060fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian ObjCProtocolDecl *rProto) { 34074084c306635b70f37029dca938444e6013f08684Steve Naroff if (lProto == rProto) 340814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return true; 34094084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), 34104084c306635b70f37029dca938444e6013f08684Steve Naroff E = rProto->protocol_end(); PI != E; ++PI) 34114084c306635b70f37029dca938444e6013f08684Steve Naroff if (ProtocolCompatibleWithProtocol(lProto, *PI)) 34124084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 34134084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 34144084c306635b70f37029dca938444e6013f08684Steve Naroff} 341514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 34164084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> 34174084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false 34184084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise. 34194084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { 34204084c306635b70f37029dca938444e6013f08684Steve Naroff if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) 34214084c306635b70f37029dca938444e6013f08684Steve Naroff return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); 34224084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 34234084c306635b70f37029dca938444e6013f08684Steve Naroff} 34244084c306635b70f37029dca938444e6013f08684Steve Naroff 34254084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an 34264084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType. 34274084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, 34284084c306635b70f37029dca938444e6013f08684Steve Naroff bool compare) { 34294084c306635b70f37029dca938444e6013f08684Steve Naroff // Allow id<P..> and an 'id' or void* type in all cases. 34304084c306635b70f37029dca938444e6013f08684Steve Naroff if (lhs->isVoidPointerType() || 34314084c306635b70f37029dca938444e6013f08684Steve Naroff lhs->isObjCIdType() || lhs->isObjCClassType()) 34324084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 34334084c306635b70f37029dca938444e6013f08684Steve Naroff else if (rhs->isVoidPointerType() || 34344084c306635b70f37029dca938444e6013f08684Steve Naroff rhs->isObjCIdType() || rhs->isObjCClassType()) 34354084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 34364084c306635b70f37029dca938444e6013f08684Steve Naroff 34374084c306635b70f37029dca938444e6013f08684Steve Naroff if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { 34384084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType(); 34394084c306635b70f37029dca938444e6013f08684Steve Naroff 34404084c306635b70f37029dca938444e6013f08684Steve Naroff if (!rhsOPT) return false; 34414084c306635b70f37029dca938444e6013f08684Steve Naroff 34424084c306635b70f37029dca938444e6013f08684Steve Naroff if (rhsOPT->qual_empty()) { 34434084c306635b70f37029dca938444e6013f08684Steve Naroff // If the RHS is a unqualified interface pointer "NSString*", 34444084c306635b70f37029dca938444e6013f08684Steve Naroff // make sure we check the class hierarchy. 34454084c306635b70f37029dca938444e6013f08684Steve Naroff if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { 34464084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 34474084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsQID->qual_end(); I != E; ++I) { 34484084c306635b70f37029dca938444e6013f08684Steve Naroff // when comparing an id<P> on lhs with a static type on rhs, 34494084c306635b70f37029dca938444e6013f08684Steve Naroff // see if static class implements all of id's protocols, directly or 34504084c306635b70f37029dca938444e6013f08684Steve Naroff // through its super class and categories. 34510fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian if (!rhsID->ClassImplementsProtocol(*I, true)) 34524084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 34534084c306635b70f37029dca938444e6013f08684Steve Naroff } 34544084c306635b70f37029dca938444e6013f08684Steve Naroff } 34554084c306635b70f37029dca938444e6013f08684Steve Naroff // If there are no qualifiers and no interface, we have an 'id'. 34564084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 34574084c306635b70f37029dca938444e6013f08684Steve Naroff } 3458de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // Both the right and left sides have qualifiers. 34594084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 34604084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsQID->qual_end(); I != E; ++I) { 34614084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *lhsProto = *I; 34624084c306635b70f37029dca938444e6013f08684Steve Naroff bool match = false; 3463de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff 3464de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // when comparing an id<P> on lhs with a static type on rhs, 3465de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // see if static class implements all of id's protocols, directly or 3466de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // through its super class and categories. 34674084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), 34684084c306635b70f37029dca938444e6013f08684Steve Naroff E = rhsOPT->qual_end(); J != E; ++J) { 34694084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *rhsProto = *J; 34704084c306635b70f37029dca938444e6013f08684Steve Naroff if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || 34714084c306635b70f37029dca938444e6013f08684Steve Naroff (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { 34724084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 34738f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff break; 34748f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff } 3475de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff } 34764084c306635b70f37029dca938444e6013f08684Steve Naroff // If the RHS is a qualified interface pointer "NSString<P>*", 34774084c306635b70f37029dca938444e6013f08684Steve Naroff // make sure we check the class hierarchy. 34784084c306635b70f37029dca938444e6013f08684Steve Naroff if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { 34794084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 34804084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsQID->qual_end(); I != E; ++I) { 34814084c306635b70f37029dca938444e6013f08684Steve Naroff // when comparing an id<P> on lhs with a static type on rhs, 34824084c306635b70f37029dca938444e6013f08684Steve Naroff // see if static class implements all of id's protocols, directly or 34834084c306635b70f37029dca938444e6013f08684Steve Naroff // through its super class and categories. 34840fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian if (rhsID->ClassImplementsProtocol(*I, true)) { 34854084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 34864084c306635b70f37029dca938444e6013f08684Steve Naroff break; 34874084c306635b70f37029dca938444e6013f08684Steve Naroff } 34884084c306635b70f37029dca938444e6013f08684Steve Naroff } 34894084c306635b70f37029dca938444e6013f08684Steve Naroff } 34904084c306635b70f37029dca938444e6013f08684Steve Naroff if (!match) 3491de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff return false; 3492de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff } 34934084c306635b70f37029dca938444e6013f08684Steve Naroff 3494de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff return true; 3495de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff } 34964084c306635b70f37029dca938444e6013f08684Steve Naroff 34974084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); 34984084c306635b70f37029dca938444e6013f08684Steve Naroff assert(rhsQID && "One of the LHS/RHS should be id<x>"); 34994084c306635b70f37029dca938444e6013f08684Steve Naroff 35004084c306635b70f37029dca938444e6013f08684Steve Naroff if (const ObjCObjectPointerType *lhsOPT = 35014084c306635b70f37029dca938444e6013f08684Steve Naroff lhs->getAsObjCInterfacePointerType()) { 35024084c306635b70f37029dca938444e6013f08684Steve Naroff if (lhsOPT->qual_empty()) { 35034084c306635b70f37029dca938444e6013f08684Steve Naroff bool match = false; 35044084c306635b70f37029dca938444e6013f08684Steve Naroff if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { 35054084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), 35064084c306635b70f37029dca938444e6013f08684Steve Naroff E = rhsQID->qual_end(); I != E; ++I) { 35074084c306635b70f37029dca938444e6013f08684Steve Naroff // when comparing an id<P> on lhs with a static type on rhs, 35084084c306635b70f37029dca938444e6013f08684Steve Naroff // see if static class implements all of id's protocols, directly or 35094084c306635b70f37029dca938444e6013f08684Steve Naroff // through its super class and categories. 35100fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian if (lhsID->ClassImplementsProtocol(*I, true)) { 35114084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 35124084c306635b70f37029dca938444e6013f08684Steve Naroff break; 35134084c306635b70f37029dca938444e6013f08684Steve Naroff } 35144084c306635b70f37029dca938444e6013f08684Steve Naroff } 35154084c306635b70f37029dca938444e6013f08684Steve Naroff if (!match) 35164084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 35174084c306635b70f37029dca938444e6013f08684Steve Naroff } 35184084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 35194084c306635b70f37029dca938444e6013f08684Steve Naroff } 35204084c306635b70f37029dca938444e6013f08684Steve Naroff // Both the right and left sides have qualifiers. 35214084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), 35224084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsOPT->qual_end(); I != E; ++I) { 35234084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *lhsProto = *I; 35244084c306635b70f37029dca938444e6013f08684Steve Naroff bool match = false; 35254084c306635b70f37029dca938444e6013f08684Steve Naroff 35264084c306635b70f37029dca938444e6013f08684Steve Naroff // when comparing an id<P> on lhs with a static type on rhs, 35274084c306635b70f37029dca938444e6013f08684Steve Naroff // see if static class implements all of id's protocols, directly or 35284084c306635b70f37029dca938444e6013f08684Steve Naroff // through its super class and categories. 35294084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), 35304084c306635b70f37029dca938444e6013f08684Steve Naroff E = rhsQID->qual_end(); J != E; ++J) { 35314084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *rhsProto = *J; 35324084c306635b70f37029dca938444e6013f08684Steve Naroff if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || 35334084c306635b70f37029dca938444e6013f08684Steve Naroff (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { 35344084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 35354084c306635b70f37029dca938444e6013f08684Steve Naroff break; 35364084c306635b70f37029dca938444e6013f08684Steve Naroff } 35374084c306635b70f37029dca938444e6013f08684Steve Naroff } 35384084c306635b70f37029dca938444e6013f08684Steve Naroff if (!match) 35394084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 35404084c306635b70f37029dca938444e6013f08684Steve Naroff } 35414084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 35424084c306635b70f37029dca938444e6013f08684Steve Naroff } 35434084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 35444084c306635b70f37029dca938444e6013f08684Steve Naroff} 35454084c306635b70f37029dca938444e6013f08684Steve Naroff 35464084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are 35474084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS. This handles validation of any 35484084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS. 35494084c306635b70f37029dca938444e6013f08684Steve Naroff/// 35504084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 35514084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCObjectPointerType *RHSOPT) { 35524084c306635b70f37029dca938444e6013f08684Steve Naroff // If either type represents the built-in 'id' or 'Class' types, return true. 35534084c306635b70f37029dca938444e6013f08684Steve Naroff if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) 35544084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 35554084c306635b70f37029dca938444e6013f08684Steve Naroff 35564084c306635b70f37029dca938444e6013f08684Steve Naroff if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) 35574084c306635b70f37029dca938444e6013f08684Steve Naroff return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), 35584084c306635b70f37029dca938444e6013f08684Steve Naroff QualType(RHSOPT,0), 35594084c306635b70f37029dca938444e6013f08684Steve Naroff false); 35604084c306635b70f37029dca938444e6013f08684Steve Naroff 35614084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); 35624084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); 35634084c306635b70f37029dca938444e6013f08684Steve Naroff if (LHS && RHS) // We have 2 user-defined types. 35644084c306635b70f37029dca938444e6013f08684Steve Naroff return canAssignObjCInterfaces(LHS, RHS); 35654084c306635b70f37029dca938444e6013f08684Steve Naroff 35664084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 356714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff} 356814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 35693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 35703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const ObjCInterfaceType *RHS) { 35716ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // Verify that the base decls are compatible: the RHS must be a subclass of 35726ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // the LHS. 35736ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) 35746ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return false; 35756ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 35766ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // RHS must have a superset of the protocols in the LHS. If the LHS is not 35776ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // protocol qualified at all, then we are good. 3578c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (LHS->getNumProtocols() == 0) 35796ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return true; 35806ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 35816ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it 35826ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // isn't a superset. 3583c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (RHS->getNumProtocols() == 0) 35846ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return true; // FIXME: should return false! 35856ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 3586c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), 3587c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff LHSPE = LHS->qual_end(); 358891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff LHSPI != LHSPE; LHSPI++) { 358991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff bool RHSImplementsProtocol = false; 359091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff 359191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // If the RHS doesn't implement the protocol on the left, the types 359291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // are incompatible. 3593c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), 35944084c306635b70f37029dca938444e6013f08684Steve Naroff RHSPE = RHS->qual_end(); 35958f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff RHSPI != RHSPE; RHSPI++) { 35968f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { 359791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff RHSImplementsProtocol = true; 35988f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff break; 35998f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff } 360091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff } 360191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // FIXME: For better diagnostics, consider passing back the protocol name. 360291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff if (!RHSImplementsProtocol) 360391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff return false; 360491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff } 360591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // The RHS implements all protocols listed on the LHS. 360691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff return true; 36076ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner} 36086ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 3609389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { 3610389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff // get the "pointed to" types 361114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType(); 361214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType(); 3613389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff 361414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (!LHSOPT || !RHSOPT) 3615389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff return false; 361614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 361714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return canAssignObjCInterfaces(LHSOPT, RHSOPT) || 361814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff canAssignObjCInterfaces(RHSOPT, LHSOPT); 3619389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff} 3620389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff 3621ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, 3622ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type. 3623ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// C99 6.2.7p1: Two types have compatible types if their types are the 3624ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules. 36253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { 36263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return !mergeTypes(LHS, RHS).isNull(); 36273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman} 36283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36293d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { 36303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const FunctionType *lbase = lhs->getAsFunctionType(); 36313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const FunctionType *rbase = rhs->getAsFunctionType(); 363272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); 363372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); 36343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman bool allLTypes = true; 36353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman bool allRTypes = true; 36363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Check return type 36383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); 36393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (retType.isNull()) return QualType(); 364061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) 364161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allLTypes = false; 364261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) 364361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allRTypes = false; 36446dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump // FIXME: double check this 36452455636163fdd18581d7fdae816433f886d88213Mike Stump bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); 36462455636163fdd18581d7fdae816433f886d88213Mike Stump if (NoReturn != lbase->getNoReturnAttr()) 36472455636163fdd18581d7fdae816433f886d88213Mike Stump allLTypes = false; 36482455636163fdd18581d7fdae816433f886d88213Mike Stump if (NoReturn != rbase->getNoReturnAttr()) 36492455636163fdd18581d7fdae816433f886d88213Mike Stump allRTypes = false; 36502455636163fdd18581d7fdae816433f886d88213Mike Stump 36513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto && rproto) { // two C99 style function prototypes 3652465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && 3653465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl "C++ shouldn't be here"); 36543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman unsigned lproto_nargs = lproto->getNumArgs(); 36553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman unsigned rproto_nargs = rproto->getNumArgs(); 36563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Compatible functions must have the same number of arguments 36583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto_nargs != rproto_nargs) 36593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 36603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Variadic and non-variadic functions aren't compatible 36623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto->isVariadic() != rproto->isVariadic()) 36633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 36643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36657fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis if (lproto->getTypeQuals() != rproto->getTypeQuals()) 36667fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis return QualType(); 36677fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis 36683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Check argument compatibility 36693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman llvm::SmallVector<QualType, 10> types; 36703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman for (unsigned i = 0; i < lproto_nargs; i++) { 36713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType largtype = lproto->getArgType(i).getUnqualifiedType(); 36723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); 36733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType argtype = mergeTypes(largtype, rargtype); 36743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (argtype.isNull()) return QualType(); 36753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman types.push_back(argtype); 367661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(argtype) != getCanonicalType(largtype)) 367761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allLTypes = false; 367861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(argtype) != getCanonicalType(rargtype)) 367961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allRTypes = false; 36803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 36813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allLTypes) return lhs; 36823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allRTypes) return rhs; 36833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return getFunctionType(retType, types.begin(), types.size(), 36842455636163fdd18581d7fdae816433f886d88213Mike Stump lproto->isVariadic(), lproto->getTypeQuals(), 36852455636163fdd18581d7fdae816433f886d88213Mike Stump NoReturn); 36863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 36873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto) allRTypes = false; 36893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (rproto) allLTypes = false; 36903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 369172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor const FunctionProtoType *proto = lproto ? lproto : rproto; 36923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (proto) { 3693465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); 36943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (proto->isVariadic()) return QualType(); 36953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Check that the types are compatible with the types that 36963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // would result from default argument promotions (C99 6.7.5.3p15). 36973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // The only types actually affected are promotable integer 36983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // types and floats, which would be passed as a different 36993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // type depending on whether the prototype is visible. 37003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman unsigned proto_nargs = proto->getNumArgs(); 37013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman for (unsigned i = 0; i < proto_nargs; ++i) { 37023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType argTy = proto->getArgType(i); 37033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (argTy->isPromotableIntegerType() || 37043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman getCanonicalType(argTy).getUnqualifiedType() == FloatTy) 37053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 37063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 37073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allLTypes) return lhs; 37093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allRTypes) return rhs; 37103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return getFunctionType(retType, proto->arg_type_begin(), 37112d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump proto->getNumArgs(), proto->isVariadic(), 37122d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump proto->getTypeQuals(), NoReturn); 37133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 37143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allLTypes) return lhs; 37163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allRTypes) return rhs; 37172455636163fdd18581d7fdae816433f886d88213Mike Stump return getFunctionNoProtoType(retType, NoReturn); 37183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman} 37193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37203d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { 372143d69750e7f7b26076e7474dec8839bb777b260fBill Wendling // C++ [expr]: If an expression initially has the type "reference to T", the 372243d69750e7f7b26076e7474dec8839bb777b260fBill Wendling // type is adjusted to "T" prior to any further analysis, the expression 372343d69750e7f7b26076e7474dec8839bb777b260fBill Wendling // designates the object or function denoted by the reference, and the 37247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // expression is an lvalue unless the reference is an rvalue reference and 37257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // the expression is a function call (possibly inside parentheses). 37263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // FIXME: C++ shouldn't be going through here! The rules are different 37273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // enough that they should be handled separately. 37287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* 37297c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // shouldn't be going through here! 37306217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) 3731c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner LHS = RT->getPointeeType(); 37326217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) 3733c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner RHS = RT->getPointeeType(); 37343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType LHSCan = getCanonicalType(LHS), 37363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman RHSCan = getCanonicalType(RHS); 37373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 3738f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner // If two types are identical, they are compatible. 37393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (LHSCan == RHSCan) 37403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 37413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // If the qualifiers are different, the types aren't compatible 37435a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // Note that we handle extended qualifiers later, in the 37445a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // case for ExtQualType. 37455a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers()) 37463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 37473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 3748852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman Type::TypeClass LHSClass = LHSCan->getTypeClass(); 3749852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman Type::TypeClass RHSClass = RHSCan->getTypeClass(); 3750f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner 37511adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // We want to consider the two function types to be the same for these 37521adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // comparisons, just force one to the other. 37531adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; 37541adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; 37554c721d381fb279899337d120edd4a24d405e56b2Eli Friedman 375607d258756dc856c6987c394a0972884e6ed46765Eli Friedman // Strip off objc_gc attributes off the top level so they can be merged. 375707d258756dc856c6987c394a0972884e6ed46765Eli Friedman // This is a complete mess, but the attribute itself doesn't make much sense. 3758585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian if (RHSClass == Type::ExtQual) { 375907d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr(); 376007d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (GCAttr != QualType::GCNone) { 376186f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr(); 37628df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian // __weak attribute must appear on both declarations. 376386f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // __strong attribue is redundant if other decl is an objective-c 376486f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // object pointer (or decorated with __strong attribute); otherwise 376586f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // issue error. 376686f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) || 376786f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian (GCAttr == QualType::Strong && GCLHSAttr != GCAttr && 376814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff !LHSCan->isObjCObjectPointerType())) 37698df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian return QualType(); 37708df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian 377107d258756dc856c6987c394a0972884e6ed46765Eli Friedman RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(), 377207d258756dc856c6987c394a0972884e6ed46765Eli Friedman RHS.getCVRQualifiers()); 377307d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType Result = mergeTypes(LHS, RHS); 37748df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (!Result.isNull()) { 37758df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (Result.getObjCGCAttr() == QualType::GCNone) 37768df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = getObjCGCQualType(Result, GCAttr); 37778df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian else if (Result.getObjCGCAttr() != GCAttr) 37788df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = QualType(); 37798df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian } 378007d258756dc856c6987c394a0972884e6ed46765Eli Friedman return Result; 378107d258756dc856c6987c394a0972884e6ed46765Eli Friedman } 3782585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian } 3783585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian if (LHSClass == Type::ExtQual) { 378407d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr(); 378507d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (GCAttr != QualType::GCNone) { 37868df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr(); 37878df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian // __weak attribute must appear on both declarations. __strong 378886f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // __strong attribue is redundant if other decl is an objective-c 378986f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // object pointer (or decorated with __strong attribute); otherwise 379086f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // issue error. 379186f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) || 379286f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian (GCAttr == QualType::Strong && GCRHSAttr != GCAttr && 379314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff !RHSCan->isObjCObjectPointerType())) 37948df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian return QualType(); 379586f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian 379607d258756dc856c6987c394a0972884e6ed46765Eli Friedman LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(), 379707d258756dc856c6987c394a0972884e6ed46765Eli Friedman LHS.getCVRQualifiers()); 379807d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType Result = mergeTypes(LHS, RHS); 37998df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (!Result.isNull()) { 38008df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (Result.getObjCGCAttr() == QualType::GCNone) 38018df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = getObjCGCQualType(Result, GCAttr); 38028df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian else if (Result.getObjCGCAttr() != GCAttr) 38038df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = QualType(); 38048df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian } 3805354e53da656237e25518b3fafa3bf84de6e6f57eEli Friedman return Result; 380607d258756dc856c6987c394a0972884e6ed46765Eli Friedman } 3807585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian } 3808585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian 38094c721d381fb279899337d120edd4a24d405e56b2Eli Friedman // Same as above for arrays 3810a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) 3811a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner LHSClass = Type::ConstantArray; 3812a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) 3813a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner RHSClass = Type::ConstantArray; 3814a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner 3815213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman // Canonicalize ExtVector -> Vector. 3816213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; 3817213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; 3818ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff 38194e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff // If the canonical type classes don't match. 38204e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff if (LHSClass != RHSClass) { 38211adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, 38221adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // a signed integer type, or an unsigned integer type. 38233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (const EnumType* ETy = LHS->getAsEnumType()) { 38243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) 38253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return RHS; 3826bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman } 38273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (const EnumType* ETy = RHS->getAsEnumType()) { 38283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) 38293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 3830bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman } 38311adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner 38323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 3833ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff } 38343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 38354a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff // The canonical type classes match. 38361adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner switch (LHSClass) { 383772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base) 383872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 383972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 384072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class: 384172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def" 384272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor assert(false && "Non-canonical and dependent types shouldn't get here"); 384372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 384472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 38457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::LValueReference: 38467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::RValueReference: 384772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::MemberPointer: 384872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor assert(false && "C++ should never be in mergeTypes"); 384972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 385072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 385172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::IncompleteArray: 385272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::VariableArray: 385372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::FunctionProto: 385472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::ExtVector: 385572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor assert(false && "Types are eliminated above"); 385672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 385772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 38581adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::Pointer: 38593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman { 38603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Merge two pointer types, while trying to preserve typedef info 38616217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); 38626217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); 38633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 38643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ResultType.isNull()) return QualType(); 386507d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 386661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return LHS; 386707d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 386861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return RHS; 38693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return getPointerType(ResultType); 38703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 3871c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff case Type::BlockPointer: 3872c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff { 3873c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff // Merge two block pointer types, while trying to preserve typedef info 38746217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); 38756217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); 3876c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 3877c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff if (ResultType.isNull()) return QualType(); 3878c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 3879c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff return LHS; 3880c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 3881c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff return RHS; 3882c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff return getBlockPointerType(ResultType); 3883c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff } 38841adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::ConstantArray: 38853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman { 38863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); 38873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); 38883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) 38893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 38903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 38913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType LHSElem = getAsArrayType(LHS)->getElementType(); 38923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType RHSElem = getAsArrayType(RHS)->getElementType(); 38933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType ResultType = mergeTypes(LHSElem, RHSElem); 38943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ResultType.isNull()) return QualType(); 389561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 389661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return LHS; 389761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 389861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return RHS; 38993bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), 39003bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman ArrayType::ArraySizeModifier(), 0); 39013bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), 39023bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman ArrayType::ArraySizeModifier(), 0); 39033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const VariableArrayType* LVAT = getAsVariableArrayType(LHS); 39043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const VariableArrayType* RVAT = getAsVariableArrayType(RHS); 390561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 390661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return LHS; 390761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 390861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return RHS; 39093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (LVAT) { 39103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // FIXME: This isn't correct! But tricky to implement because 39113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // the array's size has to be the size of LHS, but the type 39123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // has to be different. 39133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 39143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 39153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (RVAT) { 39163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // FIXME: This isn't correct! But tricky to implement because 39173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // the array's size has to be the size of RHS, but the type 39183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // has to be different. 39193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return RHS; 39203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 39213bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; 39223bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; 39237e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return getIncompleteArrayType(ResultType, 39247e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArrayType::ArraySizeModifier(), 0); 39253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 39261adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::FunctionNoProto: 39273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return mergeFunctionTypes(LHS, RHS); 392872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Record: 392972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Enum: 39303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 39311adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::Builtin: 39323cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner // Only exactly equal builtin types are compatible, which is tested above. 39333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 393464cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar case Type::Complex: 393564cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar // Distinct complex types are incompatible. 393664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar return QualType(); 39373cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner case Type::Vector: 39385a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: The merged type should be an ExtVector! 39393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) 39403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 394161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return QualType(); 394261490e9a965cfee8a78c12c6802138844f04250dCedric Venet case Type::ObjCInterface: { 39435fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff // Check if the interfaces are assignment compatible. 39445a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: This should be type compatibility, e.g. whether 39455a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // "LHS x; RHS x;" at global scope is legal. 39465fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); 39475fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); 39485fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff if (LHSIface && RHSIface && 39495fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff canAssignObjCInterfaces(LHSIface, RHSIface)) 39505fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff return LHS; 39515fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff 39523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 395361490e9a965cfee8a78c12c6802138844f04250dCedric Venet } 395414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff case Type::ObjCObjectPointer: { 395514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(), 395614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff RHS->getAsObjCObjectPointerType())) 395714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return LHS; 395814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 3959bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff return QualType(); 396014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 39615a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman case Type::FixedWidthInt: 39625a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // Distinct fixed-width integers are not compatible. 39635a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return QualType(); 39645a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman case Type::ExtQual: 39655a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: ExtQual types can be compatible even if they're not 39665a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // identical! 39675a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return QualType(); 39685a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // First attempt at an implementation, but I'm not really sure it's 39695a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // right... 39705a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#if 0 39715a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ExtQualType* LQual = cast<ExtQualType>(LHSCan); 39725a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ExtQualType* RQual = cast<ExtQualType>(RHSCan); 39735a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (LQual->getAddressSpace() != RQual->getAddressSpace() || 39745a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman LQual->getObjCGCAttr() != RQual->getObjCGCAttr()) 39755a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return QualType(); 39765a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman QualType LHSBase, RHSBase, ResultType, ResCanUnqual; 39775a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman LHSBase = QualType(LQual->getBaseType(), 0); 39785a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman RHSBase = QualType(RQual->getBaseType(), 0); 39795a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType = mergeTypes(LHSBase, RHSBase); 39805a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (ResultType.isNull()) return QualType(); 39815a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType(); 39825a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (LHSCan.getUnqualifiedType() == ResCanUnqual) 39835a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return LHS; 39845a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (RHSCan.getUnqualifiedType() == ResCanUnqual) 39855a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return RHS; 39865a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace()); 39875a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr()); 39885a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers()); 39895a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return ResultType; 39905a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#endif 39917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 39927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor case Type::TemplateSpecialization: 39937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor assert(false && "Dependent types have no size"); 39947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor break; 3995ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff } 399672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 399772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 3998ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff} 39997192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek 40005426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===// 4001ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman// Integer Predicates 4002ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===// 400388054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner 4004ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) { 4005ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman if (T == BoolTy) 4006ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return 1; 4007f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { 4008f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return FWIT->getWidth(); 4009f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman } 4010f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // For builtin types, just use the standard type sizing method 4011ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return (unsigned)getTypeSize(T); 4012ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman} 4013ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman 4014ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) { 4015ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman assert(T->isSignedIntegerType() && "Unexpected type"); 4016ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman if (const EnumType* ETy = T->getAsEnumType()) 4017ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman T = ETy->getDecl()->getIntegerType(); 4018ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman const BuiltinType* BTy = T->getAsBuiltinType(); 4019ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman assert (BTy && "Unexpected signed integer type"); 4020ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman switch (BTy->getKind()) { 4021ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Char_S: 4022ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::SChar: 4023ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedCharTy; 4024ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Short: 4025ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedShortTy; 4026ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Int: 4027ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedIntTy; 4028ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Long: 4029ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedLongTy; 4030ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::LongLong: 4031ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedLongLongTy; 40322df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::Int128: 40332df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner return UnsignedInt128Ty; 4034ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman default: 4035ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman assert(0 && "Unexpected signed integer type"); 4036ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return QualType(); 4037ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman } 4038ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman} 4039ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman 40402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { } 40412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor 40422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { } 404386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 404486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 404586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===// 404686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner// Builtin Type Computation 404786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===// 404886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 404986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the 405086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters. This returns the resultant type. 405186df27bbdbb98c39ec2184695c0561209f91beddChris Lattnerstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, 405286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ASTContext::GetBuiltinTypeError &Error, 405386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner bool AllowTypeModifiers = true) { 405486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Modifiers. 405586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner int HowLong = 0; 405686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner bool Signed = false, Unsigned = false; 405786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 405886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Read the modifiers first. 405986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner bool Done = false; 406086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner while (!Done) { 406186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner switch (*Str++) { 406286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner default: Done = true; --Str; break; 406386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'S': 406486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); 406586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Signed && "Can't use 'S' modifier multiple times!"); 406686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Signed = true; 406786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 406886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'U': 406986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); 407086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Unsigned && "Can't use 'S' modifier multiple times!"); 407186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Unsigned = true; 407286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 407386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'L': 407486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong <= 2 && "Can't have LLLL modifier"); 407586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ++HowLong; 407686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 407786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 407886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 407986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 408086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType Type; 408186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 408286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Read the base type. 408386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner switch (*Str++) { 408486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner default: assert(0 && "Unknown builtin type letter!"); 408586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'v': 408686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && 408786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "Bad modifiers used with 'v'!"); 408886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.VoidTy; 408986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 409086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'f': 409186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && 409286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "Bad modifiers used with 'f'!"); 409386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.FloatTy; 409486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 409586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'd': 409686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong < 2 && !Signed && !Unsigned && 409786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "Bad modifiers used with 'd'!"); 409886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (HowLong) 409986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.LongDoubleTy; 410086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 410186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.DoubleTy; 410286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 410386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 's': 410486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && "Bad modifiers used with 's'!"); 410586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Unsigned) 410686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.UnsignedShortTy; 410786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 410886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.ShortTy; 410986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 411086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'i': 411186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (HowLong == 3) 411286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; 411386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else if (HowLong == 2) 411486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; 411586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else if (HowLong == 1) 411686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; 411786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 411886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; 411986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 412086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'c': 412186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && "Bad modifiers used with 'c'!"); 412286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Signed) 412386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.SignedCharTy; 412486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else if (Unsigned) 412586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.UnsignedCharTy; 412686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 412786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.CharTy; 412886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 412986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'b': // boolean 413086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); 413186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.BoolTy; 413286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 413386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'z': // size_t. 413486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); 413586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getSizeType(); 413686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 413786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'F': 413886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getCFConstantStringType(); 413986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 414086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'a': 414186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getBuiltinVaListType(); 414286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Type.isNull() && "builtin va list type not initialized!"); 414386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 414486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'A': 414586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // This is a "reference" to a va_list; however, what exactly 414686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // this means depends on how va_list is defined. There are two 414786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // different kinds of va_list: ones passed by value, and ones 414886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // passed by reference. An example of a by-value va_list is 414986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // x86, where va_list is a char*. An example of by-ref va_list 415086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // is x86-64, where va_list is a __va_list_tag[1]. For x86, 415186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // we want this argument to be a char*&; for x86-64, we want 415286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // it to be a __va_list_tag*. 415386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getBuiltinVaListType(); 415486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Type.isNull() && "builtin va list type not initialized!"); 415586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Type->isArrayType()) { 415686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getArrayDecayedType(Type); 415786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } else { 415886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getLValueReferenceType(Type); 415986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 416086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 416186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'V': { 416286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner char *End; 416386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner unsigned NumElements = strtoul(Str, &End, 10); 416486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(End != Str && "Missing vector size"); 416586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 416686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Str = End; 416786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 416886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); 416986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getVectorType(ElementType, NumElements); 417086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 417186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 41729a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner case 'P': 4173c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor Type = Context.getFILEType(); 4174c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor if (Type.isNull()) { 4175f711c41dd9412a8182793259d355c4f6979ed5edMike Stump Error = ASTContext::GE_Missing_stdio; 417686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return QualType(); 417786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 4178fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump break; 41799a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner case 'J': 4180f711c41dd9412a8182793259d355c4f6979ed5edMike Stump if (Signed) 4181782fa308a765aeac2acb39c4e697c937ec21185bMike Stump Type = Context.getsigjmp_bufType(); 4182f711c41dd9412a8182793259d355c4f6979ed5edMike Stump else 4183f711c41dd9412a8182793259d355c4f6979ed5edMike Stump Type = Context.getjmp_bufType(); 4184f711c41dd9412a8182793259d355c4f6979ed5edMike Stump 4185fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump if (Type.isNull()) { 4186f711c41dd9412a8182793259d355c4f6979ed5edMike Stump Error = ASTContext::GE_Missing_setjmp; 4187fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump return QualType(); 4188fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump } 4189fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump break; 4190782fa308a765aeac2acb39c4e697c937ec21185bMike Stump } 419186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 419286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (!AllowTypeModifiers) 419386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return Type; 419486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 419586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Done = false; 419686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner while (!Done) { 419786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner switch (*Str++) { 419886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner default: Done = true; --Str; break; 419986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case '*': 420086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getPointerType(Type); 420186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 420286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case '&': 420386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getLValueReferenceType(Type); 420486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 420586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // FIXME: There's no way to have a built-in with an rvalue ref arg. 420686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'C': 420786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Type.getQualifiedType(QualType::Const); 420886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 420986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 421086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 421186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 421286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return Type; 421386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner} 421486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 421586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin. 421686df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id, 421786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner GetBuiltinTypeError &Error) { 421886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner const char *TypeStr = BuiltinInfo.GetTypeString(id); 421986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 422086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner llvm::SmallVector<QualType, 8> ArgTypes; 422186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 422286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Error = GE_None; 422386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); 422486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Error != GE_None) 422586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return QualType(); 422686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner while (TypeStr[0] && TypeStr[0] != '.') { 422786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); 422886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Error != GE_None) 422986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return QualType(); 423086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 423186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Do array -> pointer decay. The builtin should use the decayed type. 423286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Ty->isArrayType()) 423386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Ty = getArrayDecayedType(Ty); 423486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 423586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ArgTypes.push_back(Ty); 423686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 423786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 423886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert((TypeStr[0] != '.' || TypeStr[1] == 0) && 423986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "'.' should only occur at end of builtin type list!"); 424086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 424186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". 424286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (ArgTypes.size() == 0 && TypeStr[0] == '.') 424386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return getFunctionNoProtoType(ResType); 424486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), 424586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner TypeStr[0] == '.', 0); 424686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner} 4247a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4248a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType 4249a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { 4250a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Perform the usual unary conversions. We do this early so that 4251a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // integral promotions to "int" can allow us to exit early, in the 4252a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // lhs == rhs check. Also, for conversion purposes, we ignore any 4253a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // qualifiers. For example, "const float" and "float" are 4254a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // equivalent. 4255a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isPromotableIntegerType()) 4256a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman lhs = getPromotedIntegerType(lhs); 4257a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman else 4258a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman lhs = lhs.getUnqualifiedType(); 4259a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isPromotableIntegerType()) 4260a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhs = getPromotedIntegerType(rhs); 4261a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman else 4262a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhs = rhs.getUnqualifiedType(); 4263a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4264a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // If both types are identical, no conversion is needed. 4265a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs == rhs) 4266a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4267a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4268a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // If either side is a non-arithmetic type (e.g. a pointer), we are done. 4269a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The caller can deal with this (e.g. pointer + int). 4270a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) 4271a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4272a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4273a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // At this point, we have two different arithmetic types. 4274a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4275a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Handle complex types first (C99 6.3.1.8p1). 4276a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isComplexType() || rhs->isComplexType()) { 4277a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // if we have an integer operand, the result is the complex type. 4278a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { 4279a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the rhs to the lhs complex type. 4280a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4281a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4282a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { 4283a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the lhs to the rhs complex type. 4284a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4285a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4286a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // This handles complex/complex, complex/float, or float/complex. 4287a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // When both operands are complex, the shorter operand is converted to the 4288a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // type of the longer, and that is the type of the result. This corresponds 4289a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // to what is done when combining two real floating-point operands. 4290a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The fun begins when size promotion occur across type domains. 4291a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // From H&S 6.3.4: When one operand is complex and the other is a real 4292a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // floating-point type, the less precise type is converted, within it's 4293a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // real or complex domain, to the precision of the other type. For example, 4294a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // when combining a "long double" with a "double _Complex", the 4295a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // "double _Complex" is promoted to "long double _Complex". 4296a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman int result = getFloatingTypeOrder(lhs, rhs); 4297a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4298a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (result > 0) { // The left side is bigger, convert rhs. 4299a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); 4300a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (result < 0) { // The right side is bigger, convert lhs. 4301a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); 4302a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4303a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // At this point, lhs and rhs have the same rank/size. Now, make sure the 4304a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // domains match. This is a requirement for our implementation, C99 4305a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // does not require this promotion. 4306a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs != rhs) { // Domains don't match, we have complex/float mix. 4307a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isRealFloatingType()) { // handle "double, _Complex double". 4308a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4309a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else { // handle "_Complex double, double". 4310a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4311a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4312a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4313a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; // The domain/size match exactly. 4314a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4315a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Now handle "real" floating types (i.e. float, double, long double). 4316a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { 4317a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // if we have an integer operand, the result is the real floating type. 4318a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isIntegerType()) { 4319a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert rhs to the lhs floating point type. 4320a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4321a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4322a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isComplexIntegerType()) { 4323a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert rhs to the complex floating point type. 4324a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return getComplexType(lhs); 4325a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4326a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isIntegerType()) { 4327a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert lhs to the rhs floating point type. 4328a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4329a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4330a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isComplexIntegerType()) { 4331a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert lhs to the complex floating point type. 4332a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return getComplexType(rhs); 4333a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4334a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // We have two real floating types, float/complex combos were handled above. 4335a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Convert the smaller operand to the bigger result. 4336a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman int result = getFloatingTypeOrder(lhs, rhs); 4337a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (result > 0) // convert the rhs 4338a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4339a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(result < 0 && "illegal float comparison"); 4340a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; // convert the lhs 4341a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4342a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { 4343a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Handle GCC complex int extension. 4344a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); 4345a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); 4346a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4347a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhsComplexInt && rhsComplexInt) { 4348a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (getIntegerTypeOrder(lhsComplexInt->getElementType(), 4349a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhsComplexInt->getElementType()) >= 0) 4350a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; // convert the rhs 4351a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4352a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (lhsComplexInt && rhs->isIntegerType()) { 4353a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the rhs to the lhs complex type. 4354a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4355a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (rhsComplexInt && lhs->isIntegerType()) { 4356a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the lhs to the rhs complex type. 4357a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4358a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4359a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4360a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Finally, we have two differing integer types. 4361a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The rules for this case are in C99 6.3.1.8 4362a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman int compare = getIntegerTypeOrder(lhs, rhs); 4363a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman bool lhsSigned = lhs->isSignedIntegerType(), 4364a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhsSigned = rhs->isSignedIntegerType(); 4365a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman QualType destType; 4366a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhsSigned == rhsSigned) { 4367a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Same signedness; use the higher-ranked type 4368a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = compare >= 0 ? lhs : rhs; 4369a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (compare != (lhsSigned ? 1 : -1)) { 4370a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The unsigned type has greater than or equal rank to the 4371a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // signed type, so use the unsigned type 4372a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = lhsSigned ? rhs : lhs; 4373a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (getIntWidth(lhs) != getIntWidth(rhs)) { 4374a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The two types are different widths; if we are here, that 4375a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // means the signed type is larger than the unsigned type, so 4376a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // use the signed type. 4377a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = lhsSigned ? lhs : rhs; 4378a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else { 4379a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The signed type is higher-ranked than the unsigned type, 4380a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // but isn't actually any bigger (like unsigned int and long 4381a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // on most 32-bit systems). Use the unsigned type corresponding 4382a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // to the signed type. 4383a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); 4384a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4385a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return destType; 4386a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman} 4387