ASTContext.cpp revision 0d8df780aef1acda5962347a32591efc629b6748
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 2632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace { 2642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor class BeforeInTranslationUnit 2652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor : std::binary_function<SourceRange, SourceRange, bool> { 2662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceManager *SourceMgr; 2672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor public: 2692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } 2702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor bool operator()(SourceRange X, SourceRange Y) { 2722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); 2732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 2742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor }; 2752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor} 2762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Determine whether the given comment is a Doxygen-style comment. 2782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// 2792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Start the start of the comment text. 2802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// 2812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param End the end of the comment text. 2822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// 2832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Member whether we want to check whether this is a member comment 2842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// (which requires a < after the Doxygen-comment delimiter). Otherwise, 2852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// we only return true when we find a non-member comment. 2862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorstatic bool 2872e22253e03e175144aeb9d13350a12fd83f858beDouglas GregorisDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, 2882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor bool Member = false) { 2892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char *BufferStart 2902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; 2912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); 2922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); 2932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (End - Start < 4) 2952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return false; 2962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 2972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor assert(Start[0] == '/' && "Not a comment?"); 2982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) 2992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return false; 3002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) 3012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return false; 3022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return (Start[3] == '<') == Member; 3042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor} 3052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Retrieve the comment associated with the given declaration, if 3072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// it has one. 3082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorconst char *ASTContext::getCommentForDecl(const Decl *D) { 3092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!D) 3102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Check whether we have cached a comment string for this declaration 3132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // already. 3142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor llvm::DenseMap<const Decl *, std::string>::iterator Pos 3152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = DeclComments.find(D); 3162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Pos != DeclComments.end()) 3172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return Pos->second.c_str(); 3182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If we have an external AST source and have not yet loaded comments from 3202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // that source, do so now. 3212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (ExternalSource && !LoadedExternalComments) { 3222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::vector<SourceRange> LoadedComments; 3232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ExternalSource->ReadComments(LoadedComments); 3242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!LoadedComments.empty()) 3262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Comments.insert(Comments.begin(), LoadedComments.begin(), 3272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LoadedComments.end()); 3282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LoadedExternalComments = true; 3302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 3312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If there are no comments anywhere, we won't find anything. 3332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Comments.empty()) 3342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the declaration doesn't map directly to a location in a file, we 3372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // can't find the comment. 3382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceLocation DeclStartLoc = D->getLocStart(); 3392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) 3402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Find the comment that occurs just before this declaration. 3432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::vector<SourceRange>::iterator LastComment 3442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = std::lower_bound(Comments.begin(), Comments.end(), 3452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceRange(DeclStartLoc), 3462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor BeforeInTranslationUnit(&SourceMgr)); 3472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Decompose the location for the start of the declaration and find the 3492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // beginning of the file buffer. 3502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> DeclStartDecomp 3512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(DeclStartLoc); 3522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor const char *FileBufferStart 3532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getBufferData(DeclStartDecomp.first).first; 3542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // First check whether we have a comment for a member. 3562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (LastComment != Comments.end() && 3572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && 3582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor isDoxygenComment(SourceMgr, *LastComment, true)) { 3592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> LastCommentEndDecomp 3602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(LastComment->getEnd()); 3612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (DeclStartDecomp.first == LastCommentEndDecomp.first && 3622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) 3632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor == SourceMgr.getLineNumber(LastCommentEndDecomp.first, 3642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LastCommentEndDecomp.second)) { 3652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // The Doxygen member comment comes after the declaration starts and 3662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // is on the same line and in the same file as the declaration. This 3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // is the comment we want. 3682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::string &Result = DeclComments[D]; 3692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Result.append(FileBufferStart + 3702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceMgr.getFileOffset(LastComment->getBegin()), 3712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor FileBufferStart + LastCommentEndDecomp.second + 1); 3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return Result.c_str(); 3732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 3742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (LastComment == Comments.begin()) 3772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor --LastComment; 3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Decompose the end of the comment. 3812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> LastCommentEndDecomp 3822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(LastComment->getEnd()); 3832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the comment and the declaration aren't in the same file, then they 3852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // aren't related. 3862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (DeclStartDecomp.first != LastCommentEndDecomp.first) 3872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Check that we actually have a Doxygen comment. 3902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!isDoxygenComment(SourceMgr, *LastComment)) 3912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 3922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 3932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Compute the starting line for the declaration and for the end of the 3942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // comment (this is expensive). 3952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned DeclStartLine 3962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); 3972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned CommentEndLine 3982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getLineNumber(LastCommentEndDecomp.first, 3992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor LastCommentEndDecomp.second); 4002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the comment does not end on the line prior to the declaration, then 4022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // the comment is not associated with the declaration at all. 4032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (CommentEndLine + 1 != DeclStartLine) 4042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return 0; 4052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // We have a comment, but there may be more comments on the previous lines. 4072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Keep looking so long as the comments are still Doxygen comments and are 4082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // still adjacent. 4092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned ExpectedLine 4102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; 4112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::vector<SourceRange>::iterator FirstComment = LastComment; 4122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor while (FirstComment != Comments.begin()) { 4132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Look at the previous comment 4142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor --FirstComment; 4152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> Decomp 4162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); 4172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If this previous comment is in a different file, we're done. 4192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Decomp.first != DeclStartDecomp.first) { 4202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor break; 4222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If this comment is not a Doxygen comment, we're done. 4252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (!isDoxygenComment(SourceMgr, *FirstComment)) { 4262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor break; 4282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // If the line number is not what we expected, we're done. 4312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); 4322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor if (Line != ExpectedLine) { 4332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor break; 4352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Set the next expected line number. 4382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ExpectedLine 4392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; 4402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // The iterator range [FirstComment, LastComment] contains all of the 4432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // BCPL comments that, together, are associated with this declaration. 4442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Form a single comment block string for this declaration that concatenates 4452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // all of these comments. 4462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::string &Result = DeclComments[D]; 4472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor while (FirstComment != LastComment) { 4482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> DecompStart 4492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); 4502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor std::pair<FileID, unsigned> DecompEnd 4512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); 4522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Result.append(FileBufferStart + DecompStart.second, 4532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor FileBufferStart + DecompEnd.second + 1); 4542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor ++FirstComment; 4552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor } 4562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 4572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor // Append the last comment line. 4582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor Result.append(FileBufferStart + 4592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor SourceMgr.getFileOffset(LastComment->getBegin()), 4602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor FileBufferStart + LastCommentEndDecomp.second + 1); 4612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor return Result.c_str(); 4622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor} 4632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor 464464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===// 465464175bba1318bef7905122e9fda20cff926df78Chris Lattner// Type Sizing and Analysis 466464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===// 467a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 468b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 469b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type. 470b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { 471b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner const BuiltinType *BT = T->getAsBuiltinType(); 472b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner assert(BT && "Not a floating point type!"); 473b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner switch (BT->getKind()) { 474b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner default: assert(0 && "Not a floating point type!"); 475b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner case BuiltinType::Float: return Target.getFloatFormat(); 476b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner case BuiltinType::Double: return Target.getDoubleFormat(); 477b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); 478b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner } 479b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner} 480b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 481af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// getDeclAlign - Return a conservative estimate of the alignment of the 482af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl. Note that bitfields do not have a valid alignment, so 483af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them. 484b7d0844c887a72064b624dc6df12cbe1441f69d0Daniel Dunbarunsigned ASTContext::getDeclAlignInBytes(const Decl *D) { 485dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman unsigned Align = Target.getCharWidth(); 486dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 48740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) 488dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman Align = std::max(Align, AA->getAlignment()); 489dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 490af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { 491af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner QualType T = VD->getType(); 4926217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType* RT = T->getAs<ReferenceType>()) { 4934cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson unsigned AS = RT->getPointeeType().getAddressSpace(); 494f0930235ce58a91aa3b840bece9052f44d630536Anders Carlsson Align = Target.getPointerAlign(AS); 4954cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson } else if (!T->isIncompleteType() && !T->isFunctionType()) { 4964cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson // Incomplete or function types default to 1. 497dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) 498dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman T = cast<ArrayType>(T)->getElementType(); 499dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 500dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); 501dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman } 502af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner } 503dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman 504dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman return Align / Target.getCharWidth(); 505af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner} 506b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner 507a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits. This method 508a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types. 509d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned> 5101d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) { 5115e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump uint64_t Width=0; 5125e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump unsigned Align=8; 513a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner switch (T->getTypeClass()) { 51472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base) 51572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 51618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base) 51772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class: 51872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def" 51918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor assert(false && "Should not see dependent types"); 52072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor break; 52172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 5225d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner case Type::FunctionNoProto: 5235d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner case Type::FunctionProto: 52418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // GCC extension: alignof(function) = 32 bits 52518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = 0; 52618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = 32; 52718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor break; 52818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 52972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::IncompleteArray: 530fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::VariableArray: 53118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = 0; 53218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); 53318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor break; 53418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 5357e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor case Type::ConstantArrayWithExpr: 5367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor case Type::ConstantArrayWithoutExpr: 537fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::ConstantArray: { 5381d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const ConstantArrayType *CAT = cast<ConstantArrayType>(T); 539030d8846c7e520330007087e949f621989876e3aChris Lattner 54098be4943e8dc4f3905629a7102668960873cf863Chris Lattner std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); 5419e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = EltInfo.first*CAT->getSize().getZExtValue(); 542030d8846c7e520330007087e949f621989876e3aChris Lattner Align = EltInfo.second; 543030d8846c7e520330007087e949f621989876e3aChris Lattner break; 5445c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb } 545213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman case Type::ExtVector: 546030d8846c7e520330007087e949f621989876e3aChris Lattner case Type::Vector: { 547030d8846c7e520330007087e949f621989876e3aChris Lattner std::pair<uint64_t, unsigned> EltInfo = 54898be4943e8dc4f3905629a7102668960873cf863Chris Lattner getTypeInfo(cast<VectorType>(T)->getElementType()); 5499e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); 5504bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman Align = Width; 5516fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman // If the alignment is not a power of 2, round up to the next power of 2. 5526fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman // This happens for non-power-of-2 length vectors. 5536fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman // FIXME: this should probably be a target property. 5546fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman Align = 1 << llvm::Log2_32_Ceil(Align); 555030d8846c7e520330007087e949f621989876e3aChris Lattner break; 556030d8846c7e520330007087e949f621989876e3aChris Lattner } 5575d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner 5589e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner case Type::Builtin: 559a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner switch (cast<BuiltinType>(T)->getKind()) { 560692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner default: assert(0 && "Unknown builtin type!"); 561d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::Void: 56218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // GCC extension: alignof(void) = 8 bits. 56318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = 0; 56418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = 8; 56518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor break; 56618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 5676f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Bool: 5689e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getBoolWidth(); 5699e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getBoolAlign(); 5706f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 571692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::Char_S: 572692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::Char_U: 573692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::UChar: 5746f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::SChar: 5759e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getCharWidth(); 5769e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getCharAlign(); 5776f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 57864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis case BuiltinType::WChar: 57964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis Width = Target.getWCharWidth(); 58064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis Align = Target.getWCharAlign(); 58164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis break; 582f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char16: 583f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Width = Target.getChar16Width(); 584f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Align = Target.getChar16Align(); 585f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 586f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char32: 587f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Width = Target.getChar32Width(); 588f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith Align = Target.getChar32Align(); 589f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith break; 590692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::UShort: 5916f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Short: 5929e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getShortWidth(); 5939e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getShortAlign(); 5946f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 595692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::UInt: 5966f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Int: 5979e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getIntWidth(); 5989e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getIntAlign(); 5996f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 600692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::ULong: 6016f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Long: 6029e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getLongWidth(); 6039e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getLongAlign(); 6046f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 605692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner case BuiltinType::ULongLong: 6066f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::LongLong: 6079e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getLongLongWidth(); 6089e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getLongLongAlign(); 6096f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 610ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner case BuiltinType::Int128: 611ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner case BuiltinType::UInt128: 612ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner Width = 128; 613ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner Align = 128; // int128_t is 128-bit aligned on all targets. 614ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner break; 6156f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Float: 6169e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getFloatWidth(); 6179e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getFloatAlign(); 6186f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 6196f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::Double: 6205426bf6456a5aeac416a9150de157904d101c819Chris Lattner Width = Target.getDoubleWidth(); 6215426bf6456a5aeac416a9150de157904d101c819Chris Lattner Align = Target.getDoubleAlign(); 6226f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 6236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner case BuiltinType::LongDouble: 6249e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = Target.getLongDoubleWidth(); 6259e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Align = Target.getLongDoubleAlign(); 6266f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 6276e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl case BuiltinType::NullPtr: 6286e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) 6296e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl Align = Target.getPointerAlign(0); // == sizeof(void*) 6301590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl break; 631a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner } 632bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner break; 633f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman case Type::FixedWidthInt: 634f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // FIXME: This isn't precisely correct; the width/alignment should depend 635f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // on the available types for the target 636f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman Width = cast<FixedWidthIntType>(T)->getWidth(); 637736166b38235cf6d0ffb67638960d95fb2afcbd6Chris Lattner Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); 638f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman Align = Width; 639f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman break; 640f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian case Type::ExtQual: 64198be4943e8dc4f3905629a7102668960873cf863Chris Lattner // FIXME: Pointers into different addr spaces could have different sizes and 64298be4943e8dc4f3905629a7102668960873cf863Chris Lattner // alignment requirements: getPointerInfo should take an AddrSpace. 643f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0)); 644d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff case Type::ObjCObjectPointer: 6455426bf6456a5aeac416a9150de157904d101c819Chris Lattner Width = Target.getPointerWidth(0); 646f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner Align = Target.getPointerAlign(0); 6476f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner break; 648485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff case Type::BlockPointer: { 649485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); 650485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff Width = Target.getPointerWidth(AS); 651485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff Align = Target.getPointerAlign(AS); 652485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff break; 653485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff } 654f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner case Type::Pointer: { 655f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); 6565426bf6456a5aeac416a9150de157904d101c819Chris Lattner Width = Target.getPointerWidth(AS); 657f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner Align = Target.getPointerAlign(AS); 658f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner break; 659f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner } 6607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::LValueReference: 6617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::RValueReference: 6627ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner // "When applied to a reference or a reference type, the result is the size 6635d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner // of the referenced type." C++98 5.3.3p2: expr.sizeof. 6646f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner // FIXME: This is wrong for struct layout: a reference in a struct has 6656f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner // pointer size. 666bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); 667f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl case Type::MemberPointer: { 6681cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson // FIXME: This is ABI dependent. We use the Itanium C++ ABI. 6691cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers 6701cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson // If we ever want to support other ABIs this needs to be abstracted. 6711cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson 672f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); 6731cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson std::pair<uint64_t, unsigned> PtrDiffInfo = 6741cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson getTypeInfo(getPointerDiffType()); 6751cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson Width = PtrDiffInfo.first; 676f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl if (Pointee->isFunctionType()) 677f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl Width *= 2; 6781cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson Align = PtrDiffInfo.second; 6791cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson break; 680f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl } 6815d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner case Type::Complex: { 6825d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner // Complex types have the same alignment as their elements, but twice the 6835d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner // size. 6845d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner std::pair<uint64_t, unsigned> EltInfo = 68598be4943e8dc4f3905629a7102668960873cf863Chris Lattner getTypeInfo(cast<ComplexType>(T)->getElementType()); 6869e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner Width = EltInfo.first*2; 6875d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner Align = EltInfo.second; 6885d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner break; 6895d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner } 69044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel case Type::ObjCInterface: { 6911d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); 69244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); 69344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel Width = Layout.getSize(); 69444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel Align = Layout.getAlignment(); 69544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel break; 69644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel } 69772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Record: 69872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Enum: { 6991d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const TagType *TT = cast<TagType>(T); 7001d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar 7011d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar if (TT->getDecl()->isInvalidDecl()) { 7028389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner Width = 1; 7038389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner Align = 1; 7048389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner break; 7058389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner } 7068389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner 7071d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar if (const EnumType *ET = dyn_cast<EnumType>(TT)) 7087176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner return getTypeInfo(ET->getDecl()->getIntegerType()); 7097176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner 7101d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar const RecordType *RT = cast<RecordType>(TT); 7117176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); 7127176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner Width = Layout.getSize(); 7137176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner Align = Layout.getAlignment(); 714dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner break; 715a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner } 7167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 71718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::Typedef: { 71818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); 71940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { 72018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Align = Aligned->getAlignment(); 72118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); 72218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor } else 72318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); 7247532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor break; 7257176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner } 72618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 72718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::TypeOfExpr: 72818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() 72918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor .getTypePtr()); 73018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 73118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::TypeOf: 73218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); 73318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 734395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson case Type::Decltype: 735395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() 736395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson .getTypePtr()); 737395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson 73818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::QualifiedName: 73918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); 74018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor 74118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor case Type::TemplateSpecialization: 74218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor assert(getCanonicalType(T) != T && 74318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor "Cannot request the size of a dependent type"); 74418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // FIXME: this is likely to be wrong once we support template 74518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // aliases, since a template alias could refer to a typedef that 74618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor // has an __aligned__ attribute on it. 74718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor return getTypeInfo(getCanonicalType(T)); 74818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor } 749d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner 750464175bba1318bef7905122e9fda20cff926df78Chris Lattner assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); 7519e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner return std::make_pair(Width, Align); 752a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner} 753a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 75434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified 75534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits. This can be different than the ABI 75634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign 75734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type. 75834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) { 75934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner unsigned ABIAlign = getTypeAlign(T); 7601eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman 7611eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman // Double and long long should be naturally aligned if possible. 7621eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman if (const ComplexType* CT = T->getAsComplexType()) 7631eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman T = CT->getElementType().getTypePtr(); 7641eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman if (T->isSpecificBuiltinType(BuiltinType::Double) || 7651eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman T->isSpecificBuiltinType(BuiltinType::LongLong)) 7661eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman return std::max(ABIAlign, (unsigned)getTypeSize(T)); 7671eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman 76834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner return ABIAlign; 76934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner} 77034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner 771a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx, 772a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar const ObjCInterfaceDecl *OI, 773a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar llvm::SmallVectorImpl<FieldDecl*> &Fields) { 774a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 775a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian E = OI->ivar_end(); I != E; ++I) { 776f1690858344968358131f8d5690d9ee458883000Chris Lattner ObjCIvarDecl *IVDecl = *I; 777a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian if (!IVDecl->isInvalidDecl()) 778a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian Fields.push_back(cast<FieldDecl>(IVDecl)); 779a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian } 780a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian} 781a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian 782a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, 783a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar llvm::SmallVectorImpl<FieldDecl*> &Fields) { 784a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) 785a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar CollectObjCIvars(SuperClass, Fields); 786a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar CollectLocalObjCIvars(this, OI, Fields); 787a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar} 788a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar 7898e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars - 7908e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class. 7918e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// 7928e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 7938e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, 7948e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian bool CollectSynthesized) { 7958e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 7968e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian E = OI->ivar_end(); I != E; ++I) { 7978e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian Ivars.push_back(*I); 7988e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian } 7998e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if (CollectSynthesized) 8008e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian CollectSynthesizedIvars(OI, Ivars); 8018e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian} 8028e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8039820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 8049820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { 80517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), 80617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = PD->prop_end(); I != E; ++I) 8079820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) 8089820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian Ivars.push_back(Ivar); 8099820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian 8109820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian // Also look into nested protocols. 8119820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), 8129820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian E = PD->protocol_end(); P != E; ++P) 8139820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian CollectProtocolSynthesizedIvars(*P, Ivars); 8149820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian} 8159820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian 8169820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// CollectSynthesizedIvars - 8179820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// This routine collect synthesized ivars for the designated class. 8189820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// 8199820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, 8209820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { 82117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), 82217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = OI->prop_end(); I != E; ++I) { 8239820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) 8249820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian Ivars.push_back(Ivar); 8259820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian } 8269820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian // Also look into interface's protocol list for properties declared 8279820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian // in the protocol and whose ivars are synthesized. 8289820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), 8299820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian PE = OI->protocol_end(); P != PE; ++P) { 8309820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian ObjCProtocolDecl *PD = (*P); 8319820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian CollectProtocolSynthesizedIvars(PD, Ivars); 8329820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian } 8339820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian} 8349820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian 8358e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { 8368e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian unsigned count = 0; 83717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), 83817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = PD->prop_end(); I != E; ++I) 8398e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if ((*I)->getPropertyIvarDecl()) 8408e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian ++count; 8418e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8428e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian // Also look into nested protocols. 8438e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), 8448e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian E = PD->protocol_end(); P != E; ++P) 8458e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian count += CountProtocolSynthesizedIvars(*P); 8468e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian return count; 8478e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian} 8488e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8498e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) 8508e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian{ 8518e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian unsigned count = 0; 85217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), 85317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis E = OI->prop_end(); I != E; ++I) { 8548e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if ((*I)->getPropertyIvarDecl()) 8558e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian ++count; 8568e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian } 8578e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian // Also look into interface's protocol list for properties declared 8588e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian // in the protocol and whose ivars are synthesized. 8598e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), 8608e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian PE = OI->protocol_end(); P != PE; ++P) { 8618e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian ObjCProtocolDecl *PD = (*P); 8628e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian count += CountProtocolSynthesizedIvars(PD); 8638e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian } 8648e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian return count; 8658e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian} 8668e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian 8678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 8688a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { 8698a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator 8708a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis I = ObjCImpls.find(D); 8718a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis if (I != ObjCImpls.end()) 8728a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return cast<ObjCImplementationDecl>(I->second); 8738a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return 0; 8748a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 8758a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 8768a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { 8778a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator 8788a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis I = ObjCImpls.find(D); 8798a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis if (I != ObjCImpls.end()) 8808a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return cast<ObjCCategoryImplDecl>(I->second); 8818a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis return 0; 8828a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 8838a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis 8848a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl. 8858a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, 8868a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCImplementationDecl *ImplD) { 8878a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis assert(IFaceD && ImplD && "Passed null params"); 8888a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCImpls[IFaceD] = ImplD; 8898a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 8908a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl. 8918a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, 8928a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCCategoryImplDecl *ImplD) { 8938a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis assert(CatD && ImplD && "Passed null params"); 8948a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis ObjCImpls[CatD] = ImplD; 8958a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis} 8968a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis 897b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Allocate an uninitialized DeclaratorInfo. 898b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// 899b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// The caller should initialize the memory held by DeclaratorInfo using 900b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers. 901b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// 902b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type 903b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to 904b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to. 905b17166c8077cd900cca83a895c43b30ea6660598Argyrios KyrtzidisDeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) { 906b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis unsigned DataSize = TypeLoc::getFullDataSizeForType(T); 907b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis DeclaratorInfo *DInfo = 908b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8); 909b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis new (DInfo) DeclaratorInfo(T); 910b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis return DInfo; 911b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis} 912b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis 913b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the 914b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface. 915b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// 916b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's 917b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars. 91844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout & 919b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D, 920b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar const ObjCImplementationDecl *Impl) { 921532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar assert(!D->isForwardDecl() && "Invalid interface decl!"); 922532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar 92344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel // Look up this layout, if already laid out, return what we have. 924d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar ObjCContainerDecl *Key = 925d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; 926d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) 927d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar return *Entry; 92844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel 929453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // Add in synthesized ivar count if laying out an implementation. 930453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar if (Impl) { 93129445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson unsigned FieldCount = D->ivar_size(); 9328e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian unsigned SynthCount = CountSynthesizedIvars(D); 9338e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian FieldCount += SynthCount; 934d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar // If there aren't any sythesized ivars then reuse the interface 935453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // entry. Note we can't cache this because we simply free all 936453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // entries later; however we shouldn't look up implementations 937453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar // frequently. 9388e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian if (SynthCount == 0) 939453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar return getObjCLayout(D, 0); 940453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar } 941453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar 94229445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson const ASTRecordLayout *NewEntry = 94329445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); 94429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson ObjCLayouts[Key] = NewEntry; 94529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson 94644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel return *NewEntry; 94744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel} 94844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel 949b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout & 950b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { 951b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar return getObjCLayout(D, 0); 952b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar} 953b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar 954b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout & 955b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { 956b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar return getObjCLayout(D->getClassInterface(), D); 957b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar} 958b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar 95988a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the 960464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field 961464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information. 96298be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { 9634b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek D = D->getDefinition(*this); 9644b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek assert(D && "Cannot get layout of forward declarations!"); 9654bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman 966464175bba1318bef7905122e9fda20cff926df78Chris Lattner // Look up this layout, if already laid out, return what we have. 967ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman // Note that we can't save a reference to the entry because this function 968ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman // is recursive. 969ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman const ASTRecordLayout *Entry = ASTRecordLayouts[D]; 970464175bba1318bef7905122e9fda20cff926df78Chris Lattner if (Entry) return *Entry; 9714bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman 97229445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson const ASTRecordLayout *NewEntry = 97329445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson ASTRecordLayoutBuilder::ComputeLayout(*this, D); 974ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman ASTRecordLayouts[D] = NewEntry; 97529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson 9765d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner return *NewEntry; 977464175bba1318bef7905122e9fda20cff926df78Chris Lattner} 978464175bba1318bef7905122e9fda20cff926df78Chris Lattner 979a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===// 980a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner// Type creation/memoization methods 981a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===// 982a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 983f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { 984f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner QualType CanT = getCanonicalType(T); 985f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner if (CanT.getAddressSpace() == AddressSpace) 986f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner return T; 987b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 988b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // If we are composing extended qualifiers together, merge together into one 989b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // ExtQualType node. 990b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner unsigned CVRQuals = T.getCVRQualifiers(); 991b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner QualType::GCAttrTypes GCAttr = QualType::GCNone; 992b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner Type *TypeNode = T.getTypePtr(); 993b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 994b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { 995b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // If this type already has an address space specified, it cannot get 996b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // another one. 997b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner assert(EQT->getAddressSpace() == 0 && 998b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner "Type cannot be in multiple addr spaces!"); 999b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner GCAttr = EQT->getObjCGCAttr(); 1000b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner TypeNode = EQT->getBaseType(); 1001b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner } 1002f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner 1003b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // Check if we've already instantiated this type. 1004ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb llvm::FoldingSetNodeID ID; 10052455636163fdd18581d7fdae816433f886d88213Mike Stump ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); 1006ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb void *InsertPos = 0; 1007f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) 1008b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(EXTQy, CVRQuals); 1009b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 1010ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb // If the base type isn't canonical, this won't be a canonical type either, 1011ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb // so fill in the canonical type field. 1012ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb QualType Canonical; 1013b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (!TypeNode->isCanonical()) { 1014f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian Canonical = getAddrSpaceQualType(CanT, AddressSpace); 1015ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb 1016b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // Update InsertPos, the previous call could have invalidated it. 1017f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); 1018f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1019ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb } 1020b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner ExtQualType *New = 1021b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); 1022f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian ExtQualTypes.InsertNode(New, InsertPos); 1023ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb Types.push_back(New); 1024b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(New, CVRQuals); 1025ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb} 1026ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb 1027b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T, 1028b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner QualType::GCAttrTypes GCAttr) { 1029d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian QualType CanT = getCanonicalType(T); 1030b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (CanT.getObjCGCAttr() == GCAttr) 1031d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian return T; 1032d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 10334027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian if (T->isPointerType()) { 10346217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType Pointee = T->getAs<PointerType>()->getPointeeType(); 103558f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff if (Pointee->isAnyPointerType()) { 10364027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian QualType ResultType = getObjCGCQualType(Pointee, GCAttr); 10374027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian return getPointerType(ResultType); 10384027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian } 10394027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian } 1040b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // If we are composing extended qualifiers together, merge together into one 1041b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // ExtQualType node. 1042b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner unsigned CVRQuals = T.getCVRQualifiers(); 1043b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner Type *TypeNode = T.getTypePtr(); 1044b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner unsigned AddressSpace = 0; 1045b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner 1046b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { 10472455636163fdd18581d7fdae816433f886d88213Mike Stump // If this type already has an ObjCGC specified, it cannot get 1048b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // another one. 1049b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner assert(EQT->getObjCGCAttr() == QualType::GCNone && 10502455636163fdd18581d7fdae816433f886d88213Mike Stump "Type cannot have multiple ObjCGCs!"); 1051b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner AddressSpace = EQT->getAddressSpace(); 1052b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner TypeNode = EQT->getBaseType(); 1053b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner } 1054d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 1055d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian // Check if we've already instantiated an gc qual'd type of this type. 1056d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian llvm::FoldingSetNodeID ID; 10572455636163fdd18581d7fdae816433f886d88213Mike Stump ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); 1058d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian void *InsertPos = 0; 1059d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) 1060b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(EXTQy, CVRQuals); 1061d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 1062d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian // If the base type isn't canonical, this won't be a canonical type either, 1063d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian // so fill in the canonical type field. 10645a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: Isn't this also not canonical if the base type is a array 10655a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // or pointer type? I can't find any documentation for objc_gc, though... 1066d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian QualType Canonical; 1067d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian if (!T->isCanonical()) { 1068b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner Canonical = getObjCGCQualType(CanT, GCAttr); 1069d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian 1070b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner // Update InsertPos, the previous call could have invalidated it. 1071d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); 1072d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1073d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian } 1074b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner ExtQualType *New = 1075b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); 1076d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian ExtQualTypes.InsertNode(New, InsertPos); 1077d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian Types.push_back(New); 1078b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return QualType(New, CVRQuals); 1079d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian} 1080a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner 10812455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getNoReturnType(QualType T) { 10826dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump QualifierSet qs; 10836dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump qs.strip(T); 10842455636163fdd18581d7fdae816433f886d88213Mike Stump if (T->isPointerType()) { 10856217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType Pointee = T->getAs<PointerType>()->getPointeeType(); 10862455636163fdd18581d7fdae816433f886d88213Mike Stump QualType ResultType = getNoReturnType(Pointee); 10876dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType = getPointerType(ResultType); 10886dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType.setCVRQualifiers(T.getCVRQualifiers()); 10896dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump return qs.apply(ResultType, *this); 10902455636163fdd18581d7fdae816433f886d88213Mike Stump } 10912455636163fdd18581d7fdae816433f886d88213Mike Stump if (T->isBlockPointerType()) { 10926217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType(); 10932455636163fdd18581d7fdae816433f886d88213Mike Stump QualType ResultType = getNoReturnType(Pointee); 10946dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType = getBlockPointerType(ResultType); 10956dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump ResultType.setCVRQualifiers(T.getCVRQualifiers()); 10966dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump return qs.apply(ResultType, *this); 10972455636163fdd18581d7fdae816433f886d88213Mike Stump } 10982455636163fdd18581d7fdae816433f886d88213Mike Stump if (!T->isFunctionType()) 10992455636163fdd18581d7fdae816433f886d88213Mike Stump assert(0 && "can't noreturn qualify non-pointer to function or block type"); 11002455636163fdd18581d7fdae816433f886d88213Mike Stump 1101e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump if (const FunctionNoProtoType *F = T->getAsFunctionNoProtoType()) { 11022455636163fdd18581d7fdae816433f886d88213Mike Stump return getFunctionNoProtoType(F->getResultType(), true); 11032455636163fdd18581d7fdae816433f886d88213Mike Stump } 1104e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump const FunctionProtoType *F = T->getAsFunctionProtoType(); 11052455636163fdd18581d7fdae816433f886d88213Mike Stump return getFunctionType(F->getResultType(), F->arg_type_begin(), 11062455636163fdd18581d7fdae816433f886d88213Mike Stump F->getNumArgs(), F->isVariadic(), F->getTypeQuals(), 11072455636163fdd18581d7fdae816433f886d88213Mike Stump F->hasExceptionSpec(), F->hasAnyExceptionSpec(), 11082455636163fdd18581d7fdae816433f886d88213Mike Stump F->getNumExceptions(), F->exception_begin(), true); 11092455636163fdd18581d7fdae816433f886d88213Mike Stump} 11102455636163fdd18581d7fdae816433f886d88213Mike Stump 11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex 11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type. 11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) { 11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique pointers, to guarantee there is only one pointer of a particular 11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ComplexType::Profile(ID, T); 11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) 11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(CT, 0); 11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the pointee type isn't canonical, this won't be a canonical type either, 11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!T->isCanonical()) { 1127f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getComplexType(getCanonicalType(T)); 11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); 1131f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1133f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff ComplexType *New = new (*this,8) ComplexType(T, Canonical); 11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ComplexTypes.InsertNode(New, InsertPos); 11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1139f98aba35e6c3da5aae61843fc01334939e4e12ecEli FriedmanQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { 1140f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? 1141f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; 1142f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman FixedWidthIntType *&Entry = Map[Width]; 1143f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman if (!Entry) 1144f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman Entry = new FixedWidthIntType(Width, Signed); 1145f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return QualType(Entry, 0); 1146f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman} 11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to 11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type. 11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) { 11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique pointers, to guarantee there is only one pointer of a particular 11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PointerType::Profile(ID, T); 11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(PT, 0); 11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the pointee type isn't canonical, this won't be a canonical type either, 11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!T->isCanonical()) { 1164f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getPointerType(getCanonicalType(T)); 11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1168f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1170f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff PointerType *New = new (*this,8) PointerType(T, Canonical); 11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PointerTypes.InsertNode(New, InsertPos); 11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 11765618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// getBlockPointerType - Return the uniqued reference to the type for 11775618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block. 11785618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) { 1179296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff assert(T->isFunctionType() && "block of function types only"); 1180296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff // Unique pointers, to guarantee there is only one block of a particular 11815618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff // structure. 11825618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff llvm::FoldingSetNodeID ID; 11835618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerType::Profile(ID, T); 11845618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 11855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff void *InsertPos = 0; 11865618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff if (BlockPointerType *PT = 11875618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 11885618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff return QualType(PT, 0); 11895618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 1190296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff // If the block pointee type isn't canonical, this won't be a canonical 11915618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff // type either so fill in the canonical type field. 11925618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff QualType Canonical; 11935618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff if (!T->isCanonical()) { 11945618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff Canonical = getBlockPointerType(getCanonicalType(T)); 11955618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 11965618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff // Get the new insert position for the node we care about. 11975618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerType *NewIP = 11985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1199f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 12005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff } 1201f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical); 12025618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff Types.push_back(New); 12035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff BlockPointerTypes.InsertNode(New, InsertPos); 12045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff return QualType(New, 0); 12055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff} 12065618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff 12077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an 12087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type. 12097c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getLValueReferenceType(QualType T) { 12105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique pointers, to guarantee there is only one pointer of a particular 12115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 12125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ReferenceType::Profile(ID, T); 12145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 12167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl if (LValueReferenceType *RT = 12177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(RT, 0); 12197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the referencee type isn't canonical, this won't be a canonical type 12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // either, so fill in the canonical type field. 12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!T->isCanonical()) { 12247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl Canonical = getLValueReferenceType(getCanonicalType(T)); 12257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // Get the new insert position for the node we care about. 12277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceType *NewIP = 12287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 12297c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 12307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl } 12317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical); 12337c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl Types.push_back(New); 12347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl LValueReferenceTypes.InsertNode(New, InsertPos); 12357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl return QualType(New, 0); 12367c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl} 12377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an 12397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type. 12407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) { 12417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // Unique pointers, to guarantee there is only one pointer of a particular 12427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // structure. 12437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl llvm::FoldingSetNodeID ID; 12447c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl ReferenceType::Profile(ID, T); 12457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl void *InsertPos = 0; 12477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl if (RValueReferenceType *RT = 12487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) 12497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl return QualType(RT, 0); 12507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // If the referencee type isn't canonical, this won't be a canonical type 12527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // either, so fill in the canonical type field. 12537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl QualType Canonical; 12547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl if (!T->isCanonical()) { 12557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl Canonical = getRValueReferenceType(getCanonicalType(T)); 12567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl 12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 12587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceType *NewIP = 12597c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); 1260f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 12615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 12625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 12637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical); 12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 12657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl RValueReferenceTypes.InsertNode(New, InsertPos); 12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 12675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 12685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1269f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a 1270f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class. 1271f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian RedlQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) 1272f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl{ 1273f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // Unique pointers, to guarantee there is only one pointer of a particular 1274f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // structure. 1275f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl llvm::FoldingSetNodeID ID; 1276f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerType::Profile(ID, T, Cls); 1277f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1278f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl void *InsertPos = 0; 1279f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl if (MemberPointerType *PT = 1280f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1281f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl return QualType(PT, 0); 1282f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1283f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // If the pointee or class type isn't canonical, this won't be a canonical 1284f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // type either, so fill in the canonical type field. 1285f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl QualType Canonical; 1286f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl if (!T->isCanonical()) { 1287f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); 1288f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1289f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl // Get the new insert position for the node we care about. 1290f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerType *NewIP = 1291f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); 1292f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 1293f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl } 1294f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical); 1295f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl Types.push_back(New); 1296f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl MemberPointerTypes.InsertNode(New, InsertPos); 1297f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl return QualType(New, 0); 1298f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl} 1299f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl 1300fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// getConstantArrayType - Return the unique reference to the type for an 1301fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type. 1302fb22d96692c5240fb8d611290dbf7eeed3759c73Steve NaroffQualType ASTContext::getConstantArrayType(QualType EltTy, 130338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner const llvm::APInt &ArySizeIn, 1304c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff ArrayType::ArraySizeModifier ASM, 1305c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff unsigned EltTypeQuals) { 1306587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) && 1307587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman "Constant array of VLAs is illegal!"); 1308587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman 130938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner // Convert the array size into a canonical width matching the pointer size for 131038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner // the target. 131138aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner llvm::APInt ArySize(ArySizeIn); 131238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); 131338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner 13145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 13150be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); 13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 13187192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek if (ConstantArrayType *ATP = 13197192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 13205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(ATP, 0); 13215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the element type isn't canonical, this won't be a canonical type either, 13235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 13245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 13255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!EltTy->isCanonical()) { 1326f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, 1327c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff ASM, EltTypeQuals); 13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 13297192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayType *NewIP = 13307192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1331f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1334566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek ConstantArrayType *New = 1335f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); 13367192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek ConstantArrayTypes.InsertNode(New, InsertPos); 13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 13417e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithExprType - Return a reference to the type for 13427e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type. 13437e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType 13447e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithExprType(QualType EltTy, 13457e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor const llvm::APInt &ArySizeIn, 13467e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Expr *ArySizeExpr, 13477e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArrayType::ArraySizeModifier ASM, 13487e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals, 13497e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor SourceRange Brackets) { 13507e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Convert the array size into a canonical width matching the pointer 13517e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // size for the target. 13527e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor llvm::APInt ArySize(ArySizeIn); 13537e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); 13547e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 13557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Compute the canonical ConstantArrayType. 13567e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor QualType Canonical = getConstantArrayType(getCanonicalType(EltTy), 13577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ASM, EltTypeQuals); 13587e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Since we don't unique expressions, it isn't possible to unique VLA's 13597e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // that have an expression provided for their size. 13607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ConstantArrayWithExprType *New = 13617e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor new(*this,8)ConstantArrayWithExprType(EltTy, Canonical, 13627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ArySizeExpr, 13637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ASM, EltTypeQuals, Brackets); 13647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Types.push_back(New); 13657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return QualType(New, 0); 13667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor} 13677e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 13687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithoutExprType - Return a reference to the type for 13697e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type. 13707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType 13717e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithoutExprType(QualType EltTy, 13727e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor const llvm::APInt &ArySizeIn, 13737e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArrayType::ArraySizeModifier ASM, 13747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals) { 13757e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Convert the array size into a canonical width matching the pointer 13767e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // size for the target. 13777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor llvm::APInt ArySize(ArySizeIn); 13787e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); 13797e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 13807e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor // Compute the canonical ConstantArrayType. 13817e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor QualType Canonical = getConstantArrayType(getCanonicalType(EltTy), 13827e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ASM, EltTypeQuals); 13837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ConstantArrayWithoutExprType *New = 13847e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical, 13857e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArySize, ASM, EltTypeQuals); 13867e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Types.push_back(New); 13877e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return QualType(New, 0); 13887e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor} 13897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor 1390bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a 1391bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type. 13927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy, 13937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Expr *NumElts, 1394c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff ArrayType::ArraySizeModifier ASM, 13957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals, 13967e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor SourceRange Brackets) { 1397c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // Since we don't unique expressions, it isn't possible to unique VLA's 1398c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // that have an expression provided for their size. 1399c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1400566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek VariableArrayType *New = 14017e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor new(*this,8)VariableArrayType(EltTy, QualType(), 14027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor NumElts, ASM, EltTypeQuals, Brackets); 1403c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1404c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman VariableArrayTypes.push_back(New); 1405c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman Types.push_back(New); 1406c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman return QualType(New, 0); 1407c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman} 1408c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1409898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to 1410898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element 141104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type. 14127e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy, 14137e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor Expr *NumElts, 1414898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor ArrayType::ArraySizeModifier ASM, 14157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor unsigned EltTypeQuals, 14167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor SourceRange Brackets) { 1417898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && 1418898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor "Size must be type- or value-dependent!"); 1419898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 142004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor llvm::FoldingSetNodeID ID; 142104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, 142204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor EltTypeQuals, NumElts); 1423898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 142404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor void *InsertPos = 0; 142504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayType *Canon 142604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 142704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayType *New; 142804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor if (Canon) { 142904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor // We already have a canonical version of this array type; use it as 143004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor // the canonical type for a newly-built type. 143104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor New = new (*this,8) DependentSizedArrayType(*this, EltTy, 143204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor QualType(Canon, 0), 143304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor NumElts, ASM, EltTypeQuals, 143404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor Brackets); 143504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } else { 143604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor QualType CanonEltTy = getCanonicalType(EltTy); 143704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor if (CanonEltTy == EltTy) { 143804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor New = new (*this,8) DependentSizedArrayType(*this, EltTy, QualType(), 143904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor NumElts, ASM, EltTypeQuals, 144004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor Brackets); 144104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor DependentSizedArrayTypes.InsertNode(New, InsertPos); 144204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } else { 144304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, 144404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor ASM, EltTypeQuals, 144504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor SourceRange()); 144604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor New = new (*this,8) DependentSizedArrayType(*this, EltTy, Canon, 144704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor NumElts, ASM, EltTypeQuals, 144804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor Brackets); 144904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } 145004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor } 145104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor 1452898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor Types.push_back(New); 1453898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return QualType(New, 0); 1454898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor} 1455898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 1456c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy, 1457c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman ArrayType::ArraySizeModifier ASM, 1458c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman unsigned EltTypeQuals) { 1459c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman llvm::FoldingSetNodeID ID; 14600be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); 1461c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1462c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman void *InsertPos = 0; 1463c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman if (IncompleteArrayType *ATP = 1464c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) 1465c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman return QualType(ATP, 0); 1466c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1467c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // If the element type isn't canonical, this won't be a canonical type 1468c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // either, so fill in the canonical type field. 1469c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman QualType Canonical; 1470c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1471c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman if (!EltTy->isCanonical()) { 1472f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getIncompleteArrayType(getCanonicalType(EltTy), 14732bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek ASM, EltTypeQuals); 1474c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1475c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // Get the new insert position for the node we care about. 1476c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayType *NewIP = 1477c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); 1478f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 14792bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek } 1480c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 14817e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor IncompleteArrayType *New 14827e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor = new (*this,8) IncompleteArrayType(EltTy, Canonical, 14837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ASM, EltTypeQuals); 1484c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman 1485c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman IncompleteArrayTypes.InsertNode(New, InsertPos); 1486c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman Types.push_back(New); 1487c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman return QualType(New, 0); 1488fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff} 1489fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff 149073322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of 149173322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type. 149273322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { 14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer BuiltinType *baseType; 14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1495f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 149673322924127c873c13101b705dd823f5539ffa5fSteve Naroff assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); 14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Check if we've already instantiated a vector of this type. 14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 150073322924127c873c13101b705dd823f5539ffa5fSteve Naroff VectorType::Profile(ID, vecType, NumElts, Type::Vector); 15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(VTP, 0); 15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If the element type isn't canonical, this won't be a canonical type either, 15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // so fill in the canonical type field. 15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!vecType->isCanonical()) { 1509f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getVectorType(getCanonicalType(vecType), NumElts); 15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1513f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1515f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical); 15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer VectorTypes.InsertNode(New, InsertPos); 15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1521213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of 152273322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type. 1523213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { 152473322924127c873c13101b705dd823f5539ffa5fSteve Naroff BuiltinType *baseType; 152573322924127c873c13101b705dd823f5539ffa5fSteve Naroff 1526f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); 1527213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); 152873322924127c873c13101b705dd823f5539ffa5fSteve Naroff 152973322924127c873c13101b705dd823f5539ffa5fSteve Naroff // Check if we've already instantiated a vector of this type. 153073322924127c873c13101b705dd823f5539ffa5fSteve Naroff llvm::FoldingSetNodeID ID; 1531213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); 153273322924127c873c13101b705dd823f5539ffa5fSteve Naroff void *InsertPos = 0; 153373322924127c873c13101b705dd823f5539ffa5fSteve Naroff if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) 153473322924127c873c13101b705dd823f5539ffa5fSteve Naroff return QualType(VTP, 0); 153573322924127c873c13101b705dd823f5539ffa5fSteve Naroff 153673322924127c873c13101b705dd823f5539ffa5fSteve Naroff // If the element type isn't canonical, this won't be a canonical type either, 153773322924127c873c13101b705dd823f5539ffa5fSteve Naroff // so fill in the canonical type field. 153873322924127c873c13101b705dd823f5539ffa5fSteve Naroff QualType Canonical; 153973322924127c873c13101b705dd823f5539ffa5fSteve Naroff if (!vecType->isCanonical()) { 1540213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); 154173322924127c873c13101b705dd823f5539ffa5fSteve Naroff 154273322924127c873c13101b705dd823f5539ffa5fSteve Naroff // Get the new insert position for the node we care about. 154373322924127c873c13101b705dd823f5539ffa5fSteve Naroff VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); 1544f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 154573322924127c873c13101b705dd823f5539ffa5fSteve Naroff } 1546f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical); 154773322924127c873c13101b705dd823f5539ffa5fSteve Naroff VectorTypes.InsertNode(New, InsertPos); 154873322924127c873c13101b705dd823f5539ffa5fSteve Naroff Types.push_back(New); 154973322924127c873c13101b705dd823f5539ffa5fSteve Naroff return QualType(New, 0); 155073322924127c873c13101b705dd823f5539ffa5fSteve Naroff} 155173322924127c873c13101b705dd823f5539ffa5fSteve Naroff 15529cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas GregorQualType ASTContext::getDependentSizedExtVectorType(QualType vecType, 15539cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor Expr *SizeExpr, 15549cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor SourceLocation AttrLoc) { 15552ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor llvm::FoldingSetNodeID ID; 15562ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), 15572ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SizeExpr); 15582ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor 15592ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor void *InsertPos = 0; 15602ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorType *Canon 15612ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); 15622ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorType *New; 15632ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor if (Canon) { 15642ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor // We already have a canonical version of this array type; use it as 15652ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor // the canonical type for a newly-built type. 15662ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor New = new (*this,8) DependentSizedExtVectorType(*this, vecType, 15672ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType(Canon, 0), 15682ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SizeExpr, AttrLoc); 15692ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } else { 15702ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType CanonVecTy = getCanonicalType(vecType); 15712ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor if (CanonVecTy == vecType) { 15722ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor New = new (*this,8) DependentSizedExtVectorType(*this, vecType, 15732ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType(), SizeExpr, 15742ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor AttrLoc); 15752ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor DependentSizedExtVectorTypes.InsertNode(New, InsertPos); 15762ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } else { 15772ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, 15782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SourceLocation()); 15792ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor New = new (*this,8) DependentSizedExtVectorType(*this, vecType, Canon, 15802ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor SizeExpr, AttrLoc); 15812ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } 15822ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor } 15832ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor 15849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor Types.push_back(New); 15859cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor return QualType(New, 0); 15869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor} 15879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor 158872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 15902455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { 15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique functions, to guarantee there is only one function of a particular 15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 15942455636163fdd18581d7fdae816433f886d88213Mike Stump FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); 15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 159772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor if (FunctionNoProtoType *FT = 159872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(FT, 0); 16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!ResultTy->isCanonical()) { 16032455636163fdd18581d7fdae816433f886d88213Mike Stump Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); 16045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 160672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoType *NewIP = 160772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1608f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16112455636163fdd18581d7fdae816433f886d88213Mike Stump FunctionNoProtoType *New 16122455636163fdd18581d7fdae816433f886d88213Mike Stump = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn); 16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(New); 161472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionNoProtoTypes.InsertNode(New, InsertPos); 16155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(New, 0); 16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument 16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list. isVariadic indicates whether the argument list includes '...'. 162061710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, 1621971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis unsigned NumArgs, bool isVariadic, 1622465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl unsigned TypeQuals, bool hasExceptionSpec, 1623465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl bool hasAnyExceptionSpec, unsigned NumExs, 16242455636163fdd18581d7fdae816433f886d88213Mike Stump const QualType *ExArray, bool NoReturn) { 16255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Unique functions, to guarantee there is only one function of a particular 16265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // structure. 16275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::FoldingSetNodeID ID; 162872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, 1629465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, 16302455636163fdd18581d7fdae816433f886d88213Mike Stump NumExs, ExArray, NoReturn); 16315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer void *InsertPos = 0; 163372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor if (FunctionProtoType *FTP = 163472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) 16355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(FTP, 0); 1636465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 1637465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // Determine whether the type being created is already canonical or not. 16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer bool isCanonical = ResultTy->isCanonical(); 1639465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl if (hasExceptionSpec) 1640465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl isCanonical = false; 16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; i != NumArgs && isCanonical; ++i) 16425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!ArgArray[i]->isCanonical()) 16435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer isCanonical = false; 16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // If this type isn't canonical, get the canonical version of it. 1646465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // The exception spec is not part of the canonical type. 16475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer QualType Canonical; 16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (!isCanonical) { 16495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::SmallVector<QualType, 16> CanonicalArgs; 16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CanonicalArgs.reserve(NumArgs); 16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer for (unsigned i = 0; i != NumArgs; ++i) 1652f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); 1653465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 1654f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Canonical = getFunctionType(getCanonicalType(ResultTy), 1655beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad CanonicalArgs.data(), NumArgs, 165647259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor isVariadic, TypeQuals, false, 165747259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor false, 0, 0, NoReturn); 1658465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Get the new insert position for the node we care about. 166072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoType *NewIP = 166172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); 1662f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; 16635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1664465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl 166572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor // FunctionProtoType objects are allocated with extra bytes after them 1666465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // for two variable size arrays (for parameter and exception types) at the 1667465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl // end of them. 166872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoType *FTP = 1669465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + 1670465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl NumArgs*sizeof(QualType) + 1671465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl NumExs*sizeof(QualType), 8); 167272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, 1673465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, 16742455636163fdd18581d7fdae816433f886d88213Mike Stump ExArray, NumExs, Canonical, NoReturn); 16755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(FTP); 167672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor FunctionProtoTypes.InsertNode(FTP, InsertPos); 16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(FTP, 0); 16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 16802ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the 16812ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration. 16824b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { 16831e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis assert(Decl && "Passed null for Decl param"); 16842ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 16852ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor 16861e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) 16872ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor return getTypedefType(Typedef); 1688fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor else if (isa<TemplateTypeParmDecl>(Decl)) { 1689fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor assert(false && "Template type parameter types are always available."); 16909fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else if (ObjCInterfaceDecl *ObjCInterface 16919fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump = dyn_cast<ObjCInterfaceDecl>(Decl)) 16922ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor return getObjCInterfaceType(ObjCInterface); 169349aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis 1694c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { 1695566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek if (PrevDecl) 1696566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek Decl->TypeForDecl = PrevDecl->TypeForDecl; 1697f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff else 1698f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff Decl->TypeForDecl = new (*this,8) RecordType(Record); 16999fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { 1700566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek if (PrevDecl) 1701566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek Decl->TypeForDecl = PrevDecl->TypeForDecl; 1702f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff else 1703f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff Decl->TypeForDecl = new (*this,8) EnumType(Enum); 17049fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else 17052ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor assert(false && "TypeDecl without a type?"); 170649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis 17074b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek if (!PrevDecl) Types.push_back(Decl->TypeForDecl); 170849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis return QualType(Decl->TypeForDecl, 0); 17092ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor} 17102ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor 17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the 17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl. 17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) { 17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 17155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1716f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); 171772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical); 17185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Types.push_back(Decl->TypeForDecl); 17195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return QualType(Decl->TypeForDecl, 0); 17205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1722fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template 172376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// parameter or parameter pack with the given depth, index, and (optionally) 172476e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name. 1725fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas GregorQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, 172676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson bool ParameterPack, 1727fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor IdentifierInfo *Name) { 1728fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor llvm::FoldingSetNodeID ID; 172976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); 1730fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor void *InsertPos = 0; 1731fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor TemplateTypeParmType *TypeParm 1732fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); 1733fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 1734fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor if (TypeParm) 1735fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor return QualType(TypeParm, 0); 1736fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 173776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson if (Name) { 173876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); 173976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack, 174076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson Name, Canon); 174176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson } else 174276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack); 1743fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 1744fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor Types.push_back(TypeParm); 1745fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); 1746fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 1747fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor return QualType(TypeParm, 0); 1748fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor} 1749fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor 175055f6b14230c94272efbbcdd89a92224c8db9f225Douglas GregorQualType 17517532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template, 17527532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor const TemplateArgument *Args, 17537532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor unsigned NumArgs, 17547532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualType Canon) { 1755b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor if (!Canon.isNull()) 1756b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor Canon = getCanonicalType(Canon); 1757b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor else { 1758b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor // Build the canonical template specialization type. 17591275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateName CanonTemplate = getCanonicalTemplateName(Template); 17601275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor llvm::SmallVector<TemplateArgument, 4> CanonArgs; 17611275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs.reserve(NumArgs); 17621275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 17631275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); 17641275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 17651275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Determine whether this canonical template specialization type already 17661275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // exists. 17671275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor llvm::FoldingSetNodeID ID; 17681275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationType::Profile(ID, CanonTemplate, 1769828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor CanonArgs.data(), NumArgs, *this); 17701275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 17711275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor void *InsertPos = 0; 17721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationType *Spec 17731275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); 17741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 17751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor if (!Spec) { 17761275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Allocate a new canonical template specialization type. 17771275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor void *Mem = Allocate((sizeof(TemplateSpecializationType) + 17781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor sizeof(TemplateArgument) * NumArgs), 17791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 8); 1780828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, 17811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs.data(), NumArgs, 1782b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor Canon); 17831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor Types.push_back(Spec); 17841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationTypes.InsertNode(Spec, InsertPos); 17851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor } 17861275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 1787b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor if (Canon.isNull()) 1788b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor Canon = QualType(Spec, 0); 17891275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor assert(Canon->isDependentType() && 17901275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor "Non-dependent template-id type must have a canonical type"); 1791b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor } 1792fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor 17931275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Allocate the (non-canonical) template specialization type, but don't 17941275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // try to unique it: these types typically have location information that 17951275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // we don't unique and don't want to lose. 17967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor void *Mem = Allocate((sizeof(TemplateSpecializationType) + 179740808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor sizeof(TemplateArgument) * NumArgs), 179840808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor 8); 17991275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateSpecializationType *Spec 1800828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, 1801828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor Canon); 18021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 180355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor Types.push_back(Spec); 180455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor return QualType(Spec, 0); 180555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor} 180655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor 1807e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas GregorQualType 1808ab452ba8323d1985e08bade2bced588cddf2cc28Douglas GregorASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, 1809e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor QualType NamedType) { 1810e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor llvm::FoldingSetNodeID ID; 1811ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor QualifiedNameType::Profile(ID, NNS, NamedType); 1812e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor 1813e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor void *InsertPos = 0; 1814e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor QualifiedNameType *T 1815e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); 1816e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor if (T) 1817e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor return QualType(T, 0); 1818e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor 1819ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor T = new (*this) QualifiedNameType(NNS, NamedType, 1820ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor getCanonicalType(NamedType)); 1821e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor Types.push_back(T); 1822e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor QualifiedNameTypes.InsertNode(T, InsertPos); 1823e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor return QualType(T, 0); 1824e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor} 1825e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor 1826d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, 1827d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor const IdentifierInfo *Name, 1828d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor QualType Canon) { 1829d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor assert(NNS->isDependent() && "nested-name-specifier must be dependent"); 1830d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1831d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (Canon.isNull()) { 1832d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 1833d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (CanonNNS != NNS) 1834d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor Canon = getTypenameType(CanonNNS, Name); 1835d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 1836d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1837d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor llvm::FoldingSetNodeID ID; 1838d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor TypenameType::Profile(ID, NNS, Name); 1839d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1840d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor void *InsertPos = 0; 1841d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor TypenameType *T 1842d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); 1843d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (T) 1844d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return QualType(T, 0); 1845d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 1846d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor T = new (*this) TypenameType(NNS, Name, Canon); 1847d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor Types.push_back(T); 1848d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor TypenameTypes.InsertNode(T, InsertPos); 1849d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return QualType(T, 0); 1850d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor} 1851d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 18521734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorQualType 18531734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorASTContext::getTypenameType(NestedNameSpecifier *NNS, 18541734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor const TemplateSpecializationType *TemplateId, 18551734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor QualType Canon) { 18561734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor assert(NNS->isDependent() && "nested-name-specifier must be dependent"); 18571734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18581734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor if (Canon.isNull()) { 18591734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 18601734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); 18611734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { 18621734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor const TemplateSpecializationType *CanonTemplateId 18631734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor = CanonType->getAsTemplateSpecializationType(); 18641734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor assert(CanonTemplateId && 18651734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor "Canonical type must also be a template specialization type"); 18661734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Canon = getTypenameType(CanonNNS, CanonTemplateId); 18671734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } 18681734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor } 18691734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18701734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor llvm::FoldingSetNodeID ID; 18711734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor TypenameType::Profile(ID, NNS, TemplateId); 18721734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18731734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor void *InsertPos = 0; 18741734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor TypenameType *T 18751734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); 18761734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor if (T) 18771734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor return QualType(T, 0); 18781734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 18791734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor T = new (*this) TypenameType(NNS, TemplateId, Canon); 18801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor Types.push_back(T); 18811734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor TypenameTypes.InsertNode(T, InsertPos); 18821734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor return QualType(T, 0); 18831734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor} 18841734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor 188588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols 188688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically. 188788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS, 188888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner const ObjCProtocolDecl *RHS) { 18892e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor return LHS->getDeclName() < RHS->getDeclName(); 189088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner} 189188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 189288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, 189388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner unsigned &NumProtocols) { 189488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; 189588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 189688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner // Sort protocols, keyed by name. 189788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); 189888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 189988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner // Remove duplicates. 190088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); 190188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner NumProtocols = ProtocolsEnd-Protocols; 190288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner} 190388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 1904d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for 1905d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// the given interface decl and the conforming protocol list. 190614108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, 1907d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCProtocolDecl **Protocols, 1908d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff unsigned NumProtocols) { 1909d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff // Sort the protocol list alphabetically to canonicalize it. 1910d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff if (NumProtocols) 1911d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff SortAndUniqueProtocols(Protocols, NumProtocols); 1912d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1913d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff llvm::FoldingSetNodeID ID; 191414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); 1915d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1916d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff void *InsertPos = 0; 1917d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff if (ObjCObjectPointerType *QT = 1918d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) 1919d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff return QualType(QT, 0); 1920d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1921d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff // No Match; 1922d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCObjectPointerType *QType = 192314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols); 1924d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff 1925d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff Types.push_back(QType); 1926d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff ObjCObjectPointerTypes.InsertNode(QType, InsertPos); 1927d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff return QualType(QType, 0); 1928d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff} 192988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 1930c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// getObjCInterfaceType - Return the unique reference to the type for the 1931c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// specified ObjC interface decl. The list of protocols is optional. 1932c15cb2af27514ecc879daba9aa01389c5203685dSteve NaroffQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 1933a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCProtocolDecl **Protocols, unsigned NumProtocols) { 1934c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (NumProtocols) 1935c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff // Sort the protocol list alphabetically to canonicalize it. 1936c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff SortAndUniqueProtocols(Protocols, NumProtocols); 193788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner 19384b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian llvm::FoldingSetNodeID ID; 1939c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); 19404b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian 19414b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian void *InsertPos = 0; 1942c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (ObjCInterfaceType *QT = 1943c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) 19444b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian return QualType(QT, 0); 19454b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian 19464b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian // No Match; 1947c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceType *QType = 1948c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl), 1949c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff Protocols, NumProtocols); 19504b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian Types.push_back(QType); 1951c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff ObjCInterfaceTypes.InsertNode(QType, InsertPos); 19524b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian return QualType(QType, 0); 19534b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian} 19544b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian 195572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique 195672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example, 19579752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different 19589752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// DeclRefExpr's. This doesn't effect the type checker, since it operates 19599752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique). 196072564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) { 1961dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor TypeOfExprType *toe; 1962b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor if (tofExpr->isTypeDependent()) { 1963b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor llvm::FoldingSetNodeID ID; 1964b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor DependentTypeOfExprType::Profile(ID, *this, tofExpr); 1965b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor 1966b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor void *InsertPos = 0; 1967b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor DependentTypeOfExprType *Canon 1968b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); 1969b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor if (Canon) { 1970b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor // We already have a "canonical" version of an identical, dependent 1971b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor // typeof(expr) type. Use that as our canonical type. 1972b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor toe = new (*this, 8) TypeOfExprType(tofExpr, 1973b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor QualType((TypeOfExprType*)Canon, 0)); 1974b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor } 1975b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor else { 1976b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor // Build a new, canonical typeof(expr) type. 1977b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr); 1978b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); 1979b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor toe = Canon; 1980b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor } 1981b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor } else { 1982dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor QualType Canonical = getCanonicalType(tofExpr->getType()); 1983dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor toe = new (*this,8) TypeOfExprType(tofExpr, Canonical); 1984dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor } 19859752f25748d954df99087d741ea35db37ff16beaSteve Naroff Types.push_back(toe); 19869752f25748d954df99087d741ea35db37ff16beaSteve Naroff return QualType(toe, 0); 1987d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff} 1988d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff 19899752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique 19909752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be 19919752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be 19929752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// an issue. This doesn't effect the type checker, since it operates 19939752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique). 1994d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) { 1995f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner QualType Canonical = getCanonicalType(tofType); 1996f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical); 19979752f25748d954df99087d741ea35db37ff16beaSteve Naroff Types.push_back(tot); 19989752f25748d954df99087d741ea35db37ff16beaSteve Naroff return QualType(tot, 0); 1999d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff} 2000d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff 200160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that 200260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4 200360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { 2004a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson if (e->isTypeDependent()) 2005a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson return Context.DependentTy; 2006a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson 200760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // If e is an id expression or a class member access, decltype(e) is defined 200860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // as the type of the entity named by e. 200960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { 201060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) 201160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return VD->getType(); 201260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson } 201360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { 201460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) 201560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return FD->getType(); 201660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson } 201760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // If e is a function call or an invocation of an overloaded operator, 201860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // (parentheses around e are ignored), decltype(e) is defined as the 201960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // return type of that function. 202060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) 202160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return CE->getCallReturnType(); 202260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 202360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson QualType T = e->getType(); 202460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 202560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is 202660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson // defined as T&, otherwise decltype(e) is defined as T. 202760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson if (e->isLvalue(Context) == Expr::LV_Valid) 202860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson T = Context.getLValueReferenceType(T); 202960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 203060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson return T; 203160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson} 203260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson 2033395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique 2034395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be 2035395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be 2036395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// an issue. This doesn't effect the type checker, since it operates 2037395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique). 2038395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) { 2039dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor DecltypeType *dt; 20409d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor if (e->isTypeDependent()) { 20419d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor llvm::FoldingSetNodeID ID; 20429d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor DependentDecltypeType::Profile(ID, *this, e); 20439d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor 20449d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor void *InsertPos = 0; 20459d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor DependentDecltypeType *Canon 20469d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); 20479d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor if (Canon) { 20489d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor // We already have a "canonical" version of an equivalent, dependent 20499d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor // decltype type. Use that as our canonical type. 20509d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor dt = new (*this, 8) DecltypeType(e, DependentTy, 20519d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor QualType((DecltypeType*)Canon, 0)); 20529d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor } 20539d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor else { 20549d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor // Build a new, canonical typeof(expr) type. 20559d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor Canon = new (*this, 8) DependentDecltypeType(*this, e); 20569d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor DependentDecltypeTypes.InsertNode(Canon, InsertPos); 20579d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor dt = Canon; 20589d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor } 20599d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor } else { 2060dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor QualType T = getDecltypeForExpr(e, *this); 2061563a03b1338d31c2462def43253a722bc885d384Anders Carlsson dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T)); 2062dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor } 2063395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson Types.push_back(dt); 2064395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson return QualType(dt, 0); 2065395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson} 2066395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson 20675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the 20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl. 2069e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) { 2070d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek assert (Decl); 2071e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump // FIXME: What is the design on getTagDeclType when it requires casting 2072e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump // away const? mutable? 2073e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump return getTypeDeclType(const_cast<TagDecl*>(Decl)); 20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 20765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result 20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and 20785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// needs to agree with the definition in <stddef.h>. 20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const { 2080b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor return getFromTargetType(Target.getSizeType()); 20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 20825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 208364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t". 208464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension. 208564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const { 208664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis // FIXME: derive from "Target" ? 208764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis return WCharTy; 208864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis} 208964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis 209064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t". 209164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension. 209264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const { 209364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis // FIXME: derive from "Target" ? 209464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis return UnsignedIntTy; 209564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis} 209664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis 20978b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 20988b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 20998b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const { 2100b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor return getFromTargetType(Target.getPtrDiffType(0)); 21018b9023ba35a86838789e2c9034a6128728c547aaChris Lattner} 21028b9023ba35a86838789e2c9034a6128728c547aaChris Lattner 2103e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===// 2104e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner// Type Operators 2105e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===// 2106e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 210777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to 210877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type. The non-canonical version 210977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types. Decorators can 211077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 211177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared 211277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison. 211350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) { 211477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); 2115c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2116c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If the result has type qualifiers, make sure to canonicalize them as well. 2117c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); 211850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor if (TypeQuals == 0) 211950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe(CanType); 2120c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2121c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If the type qualifiers are on an array type, get the canonical type of the 2122c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // array with the qualifiers applied to the element type. 2123c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner ArrayType *AT = dyn_cast<ArrayType>(CanType); 2124c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (!AT) 212550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe(CanType.getQualifiedType(TypeQuals)); 2126c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2127c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Get the canonical version of the element with the extra qualifiers on it. 2128c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // This can recursively sink qualifiers through multiple levels of arrays. 2129c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); 2130c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner NewEltTy = getCanonicalType(NewEltTy); 2131c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2132c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 213350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe( 213450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor getConstantArrayType(NewEltTy, CAT->getSize(), 213550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor CAT->getSizeModifier(), 213650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor CAT->getIndexTypeQualifier())); 2137c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) 213850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe( 213950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), 214050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor IAT->getIndexTypeQualifier())); 2141c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2142898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) 214350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe( 214450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor getDependentSizedArrayType(NewEltTy, 2145bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr() ? 2146bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr()->Retain() : 0, 214750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor DSAT->getSizeModifier(), 214850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor DSAT->getIndexTypeQualifier(), 214950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor DSAT->getBracketsRange())); 2150898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 2151c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner VariableArrayType *VAT = cast<VariableArrayType>(AT); 215250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, 2153bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr() ? 2154bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr()->Retain() : 0, 215550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor VAT->getSizeModifier(), 215650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor VAT->getIndexTypeQualifier(), 215750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor VAT->getBracketsRange())); 2158c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner} 2159c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 216025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { 216125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor // If this template name refers to a template, the canonical 216225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor // template name merely stores the template itself. 216325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor if (TemplateDecl *Template = Name.getAsTemplateDecl()) 216497fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); 216525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor 2166d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor // If this template name refers to a set of overloaded function templates, 2167d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor /// the canonical template name merely stores the set of function templates. 21686ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) { 21696ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor OverloadedFunctionDecl *CanonOvl = 0; 21706ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), 21716ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor FEnd = Ovl->function_end(); 21726ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor F != FEnd; ++F) { 21736ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor Decl *Canon = F->get()->getCanonicalDecl(); 21746ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor if (CanonOvl || Canon != F->get()) { 21756ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor if (!CanonOvl) 21766ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor CanonOvl = OverloadedFunctionDecl::Create(*this, 21776ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor Ovl->getDeclContext(), 21786ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor Ovl->getDeclName()); 21796ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor 21806ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor CanonOvl->addOverload( 21816ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon))); 21826ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor } 21836ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor } 21846ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor 21856ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor return TemplateName(CanonOvl? CanonOvl : Ovl); 21866ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor } 2187d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 218825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor DependentTemplateName *DTN = Name.getAsDependentTemplateName(); 218925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor assert(DTN && "Non-dependent template names must refer to template decls."); 219025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor return DTN->CanonicalTemplateName; 219125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor} 219225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor 21931275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorTemplateArgument 21941275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { 21951275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor switch (Arg.getKind()) { 21961275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Null: 21971275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return Arg; 21981275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 21991275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Expression: 22001275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // FIXME: Build canonical expression? 22011275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return Arg; 22021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Declaration: 22041275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(SourceLocation(), 22051275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor Arg.getAsDecl()->getCanonicalDecl()); 22061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22071275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Integral: 22081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(SourceLocation(), 22091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor *Arg.getAsIntegral(), 22101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor getCanonicalType(Arg.getIntegralType())); 22111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22121275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Type: 22131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(SourceLocation(), 22141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor getCanonicalType(Arg.getAsType())); 22151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor case TemplateArgument::Pack: { 22171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // FIXME: Allocate in ASTContext 22181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; 22191275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor unsigned Idx = 0; 22201275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor for (TemplateArgument::pack_iterator A = Arg.pack_begin(), 22211275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor AEnd = Arg.pack_end(); 22221275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor A != AEnd; (void)++A, ++Idx) 22231275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor CanonArgs[Idx] = getCanonicalTemplateArgument(*A); 22241275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22251275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor TemplateArgument Result; 22261275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); 22271275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return Result; 22281275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor } 22291275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor } 22301275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 22311275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor // Silence GCC warning 22321275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor assert(false && "Unhandled template argument kind"); 22331275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor return TemplateArgument(); 22341275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor} 22351275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor 2236d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier * 2237d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { 2238d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor if (!NNS) 2239d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return 0; 2240d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2241d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor switch (NNS->getKind()) { 2242d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::Identifier: 2243d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // Canonicalize the prefix but keep the identifier the same. 2244d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return NestedNameSpecifier::Create(*this, 2245d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor getCanonicalNestedNameSpecifier(NNS->getPrefix()), 2246d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor NNS->getAsIdentifier()); 2247d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2248d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::Namespace: 2249d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // A namespace is canonical; build a nested-name-specifier with 2250d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // this namespace and no prefix. 2251d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); 2252d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2253d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::TypeSpec: 2254d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::TypeSpecWithTemplate: { 2255d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); 2256495c35d291da48c4f5655bbb54d15128ddde0d4dDouglas Gregor return NestedNameSpecifier::Create(*this, 0, 2257d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, 2258d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor T.getTypePtr()); 2259d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 2260d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2261d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor case NestedNameSpecifier::Global: 2262d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // The global specifier is canonical and unique. 2263d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return NNS; 2264d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor } 2265d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2266d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor // Required to silence a GCC warning 2267d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor return 0; 2268d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor} 2269d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor 2270c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2271c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) { 2272c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Handle the non-qualified case efficiently. 2273c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (T.getCVRQualifiers() == 0) { 2274c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Handle the common positive case fast. 2275c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const ArrayType *AT = dyn_cast<ArrayType>(T)) 2276c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return AT; 2277c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } 2278c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2279c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Handle the common negative case fast, ignoring CVR qualifiers. 2280c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType CType = T->getCanonicalTypeInternal(); 2281c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2282f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian // Make sure to look through type qualifiers (like ExtQuals) for the negative 2283c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // test. 2284c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (!isa<ArrayType>(CType) && 2285c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner !isa<ArrayType>(CType.getUnqualifiedType())) 2286c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return 0; 2287c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2288c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Apply any CVR qualifiers from the array type to the element type. This 2289c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // implements C99 6.7.3p8: "If the specification of an array type includes 2290c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // any type qualifiers, the element type is so qualified, not the array type." 2291c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2292c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If we get here, we either have type qualifiers on the type, or we have 2293c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // sugar such as a typedef in the way. If we have type qualifiers on the type 229450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor // we must propagate them down into the element type. 2295c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner unsigned CVRQuals = T.getCVRQualifiers(); 2296c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner unsigned AddrSpace = 0; 2297c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner Type *Ty = T.getTypePtr(); 2298c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2299f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian // Rip through ExtQualType's and typedefs to get to a concrete type. 2300c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner while (1) { 2301f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) { 2302f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian AddrSpace = EXTQT->getAddressSpace(); 2303f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian Ty = EXTQT->getBaseType(); 2304c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } else { 2305c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner T = Ty->getDesugaredType(); 2306c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) 2307c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner break; 2308c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner CVRQuals |= T.getCVRQualifiers(); 2309c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner Ty = T.getTypePtr(); 2310c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } 2311c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner } 2312c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2313c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // If we have a simple case, just return now. 2314c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner const ArrayType *ATy = dyn_cast<ArrayType>(Ty); 2315c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) 2316c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return ATy; 2317c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2318c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Otherwise, we have an array and we have qualifiers on it. Push the 2319c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // qualifiers into the array element type and return a new array type. 2320c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Get the canonical version of the element with the extra qualifiers on it. 2321c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // This can recursively sink qualifiers through multiple levels of arrays. 2322c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType NewEltTy = ATy->getElementType(); 2323c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (AddrSpace) 2324f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace); 2325c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); 2326c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2327c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) 2328c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), 2329c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner CAT->getSizeModifier(), 2330c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner CAT->getIndexTypeQualifier())); 2331c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) 2332c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return cast<ArrayType>(getIncompleteArrayType(NewEltTy, 2333c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner IAT->getSizeModifier(), 23347e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor IAT->getIndexTypeQualifier())); 2335898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor 2336898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor if (const DependentSizedArrayType *DSAT 2337898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor = dyn_cast<DependentSizedArrayType>(ATy)) 2338898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor return cast<ArrayType>( 2339898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor getDependentSizedArrayType(NewEltTy, 2340bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr() ? 2341bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman DSAT->getSizeExpr()->Retain() : 0, 2342898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor DSAT->getSizeModifier(), 23437e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor DSAT->getIndexTypeQualifier(), 23447e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor DSAT->getBracketsRange())); 2345c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner 2346c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner const VariableArrayType *VAT = cast<VariableArrayType>(ATy); 23477e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return cast<ArrayType>(getVariableArrayType(NewEltTy, 2348bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr() ? 2349bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman VAT->getSizeExpr()->Retain() : 0, 2350c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner VAT->getSizeModifier(), 23517e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor VAT->getIndexTypeQualifier(), 23527e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor VAT->getBracketsRange())); 235377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner} 235477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner 235577c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner 2356e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the 2357e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer. This operation is non-trivial when 2358e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc. The canonical type of "T" must be an array type, 2359e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array. 2360e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// 2361e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 2362e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) { 2363c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // Get the element type with 'getAsArrayType' so that we don't lose any 2364c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // typedefs in the element type of the array. This also handles propagation 2365c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // of type qualifiers from the array type into the element type if present 2366c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner // (C99 6.7.3p8). 2367c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner const ArrayType *PrettyArrayType = getAsArrayType(Ty); 2368c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner assert(PrettyArrayType && "Not an array type!"); 2369e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 2370c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); 2371e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 2372e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner // int x[restrict 4] -> int *restrict 2373c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); 2374e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner} 2375e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner 23765e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) { 23775e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor QualifierSet qualifiers; 23785e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor while (true) { 23795e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor const Type *UT = qualifiers.strip(QT); 23805e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { 23815e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor QT = AT->getElementType(); 23826dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump } else { 23835e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor return qualifiers.apply(QT, *this); 23845e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor } 23855e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor } 23865e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor} 23875e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor 2388d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel DunbarQualType ASTContext::getBaseElementType(const VariableArrayType *VAT) { 23896183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson QualType ElemTy = VAT->getElementType(); 23906183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson 23916183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy)) 23926183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson return getBaseElementType(VAT); 23936183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson 23946183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson return ElemTy; 23956183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson} 23966183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson 23970de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements. 23980de78998e7bda473b408437053e48661b510d453Fariborz Jahanianuint64_t 23990de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { 24000de78998e7bda473b408437053e48661b510d453Fariborz Jahanian uint64_t ElementCount = 1; 24010de78998e7bda473b408437053e48661b510d453Fariborz Jahanian do { 24020de78998e7bda473b408437053e48661b510d453Fariborz Jahanian ElementCount *= CA->getSize().getZExtValue(); 24030de78998e7bda473b408437053e48661b510d453Fariborz Jahanian CA = dyn_cast<ConstantArrayType>(CA->getElementType()); 24040de78998e7bda473b408437053e48661b510d453Fariborz Jahanian } while (CA); 24050de78998e7bda473b408437053e48661b510d453Fariborz Jahanian return ElementCount; 24060de78998e7bda473b408437053e48661b510d453Fariborz Jahanian} 24070de78998e7bda473b408437053e48661b510d453Fariborz Jahanian 24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types. 24095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float. 2410a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) { 2411ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb if (const ComplexType *CT = T->getAsComplexType()) 24125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return getFloatingRank(CT->getElementType()); 2413a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner 2414d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel Dunbar assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type"); 2415ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb switch (T->getAsBuiltinType()->getKind()) { 2416a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner default: assert(0 && "getFloatingRank(): not a floating type"); 24175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Float: return FloatRank; 24185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Double: return DoubleRank; 24195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::LongDouble: return LongDoubleRank; 24205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2423716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// getFloatingTypeOfSizeWithinDomain - Returns a real floating 2424716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// point or a complex type (based on typeDomain/typeSize). 2425716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type. 2426716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type. 24271361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, 24281361b11066239ea15764a2a844405352d87296b3Chris Lattner QualType Domain) const { 24291361b11066239ea15764a2a844405352d87296b3Chris Lattner FloatingRank EltRank = getFloatingRank(Size); 24301361b11066239ea15764a2a844405352d87296b3Chris Lattner if (Domain->isComplexType()) { 24311361b11066239ea15764a2a844405352d87296b3Chris Lattner switch (EltRank) { 2432716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff default: assert(0 && "getFloatingRank(): illegal value for rank"); 2433f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff case FloatRank: return FloatComplexTy; 2434f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff case DoubleRank: return DoubleComplexTy; 2435f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff case LongDoubleRank: return LongDoubleComplexTy; 2436f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff } 2437f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff } 24381361b11066239ea15764a2a844405352d87296b3Chris Lattner 24391361b11066239ea15764a2a844405352d87296b3Chris Lattner assert(Domain->isRealFloatingType() && "Unknown domain!"); 24401361b11066239ea15764a2a844405352d87296b3Chris Lattner switch (EltRank) { 24411361b11066239ea15764a2a844405352d87296b3Chris Lattner default: assert(0 && "getFloatingRank(): illegal value for rank"); 24421361b11066239ea15764a2a844405352d87296b3Chris Lattner case FloatRank: return FloatTy; 24431361b11066239ea15764a2a844405352d87296b3Chris Lattner case DoubleRank: return DoubleTy; 24441361b11066239ea15764a2a844405352d87296b3Chris Lattner case LongDoubleRank: return LongDoubleTy; 24455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 24465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 24487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating 24497cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' == 24507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 24517cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1. 2452a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { 2453a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner FloatingRank LHSR = getFloatingRank(LHS); 2454a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner FloatingRank RHSR = getFloatingRank(RHS); 2455a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner 2456a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner if (LHSR == RHSR) 2457fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff return 0; 2458a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner if (LHSR > RHSR) 2459fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff return 1; 2460fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff return -1; 24615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 24625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2463f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This 2464f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum, 2465f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized. 2466f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) { 2467f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner assert(T->isCanonical() && "T should be canonicalized"); 2468f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman if (EnumType* ET = dyn_cast<EnumType>(T)) 2469f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman T = ET->getDecl()->getIntegerType().getTypePtr(); 2470f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman 2471a34267595534a72703290153a6f7e3da1adcec59Eli Friedman if (T->isSpecificBuiltinType(BuiltinType::WChar)) 2472a34267595534a72703290153a6f7e3da1adcec59Eli Friedman T = getFromTargetType(Target.getWCharType()).getTypePtr(); 2473a34267595534a72703290153a6f7e3da1adcec59Eli Friedman 2474f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith if (T->isSpecificBuiltinType(BuiltinType::Char16)) 2475f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith T = getFromTargetType(Target.getChar16Type()).getTypePtr(); 2476f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith 2477f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith if (T->isSpecificBuiltinType(BuiltinType::Char32)) 2478f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith T = getFromTargetType(Target.getChar32Type()).getTypePtr(); 2479f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith 2480f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // There are two things which impact the integer rank: the width, and 2481f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // the ordering of builtins. The builtin ordering is encoded in the 2482f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // bottom three bits; the width is encoded in the bits above that. 24831b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) 2484f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return FWIT->getWidth() << 3; 2485f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman 2486f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner switch (cast<BuiltinType>(T)->getKind()) { 24877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner default: assert(0 && "getIntegerRank(): not a built-in integer"); 24887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Bool: 2489f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 1 + (getIntWidth(BoolTy) << 3); 24907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Char_S: 24917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Char_U: 24927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::SChar: 24937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::UChar: 2494f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 2 + (getIntWidth(CharTy) << 3); 24957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Short: 24967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::UShort: 2497f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 3 + (getIntWidth(ShortTy) << 3); 24987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Int: 24997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::UInt: 2500f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 4 + (getIntWidth(IntTy) << 3); 25017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::Long: 25027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::ULong: 2503f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 5 + (getIntWidth(LongTy) << 3); 25047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::LongLong: 25057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner case BuiltinType::ULongLong: 2506f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return 6 + (getIntWidth(LongLongTy) << 3); 25072df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::Int128: 25082df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::UInt128: 25092df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner return 7 + (getIntWidth(Int128Ty) << 3); 2510f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner } 2511f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner} 2512f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner 251304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according 251404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 251504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// 251604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no 251704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs. 251804e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) { 251904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman FieldDecl *Field = E->getBitField(); 252004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman if (!Field) 252104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return QualType(); 252204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 252304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman QualType FT = Field->getType(); 252404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 252504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); 252604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman uint64_t BitWidth = BitWidthAP.getZExtValue(); 252704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman uint64_t IntSize = getTypeSize(IntTy); 252804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // GCC extension compatibility: if the bit-field size is less than or equal 252904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // to the size of int, it gets promoted no matter what its type is. 253004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // For instance, unsigned long bf : 4 gets promoted to signed int. 253104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman if (BitWidth < IntSize) 253204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return IntTy; 253304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 253404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman if (BitWidth == IntSize) 253504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; 253604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 253704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // Types bigger than int are not subject to promotions, and therefore act 253804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // like the base type. 253904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // FIXME: This doesn't quite match what gcc does, but what gcc does here 254004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman // is ridiculous. 254104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman return QualType(); 254204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman} 254304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman 2544a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will 2545a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 2546a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type. 2547a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) { 2548a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(!Promotable.isNull()); 2549a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(Promotable->isPromotableIntegerType()); 2550a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (Promotable->isSignedIntegerType()) 2551a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return IntTy; 2552a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman uint64_t PromotableSize = getTypeSize(Promotable); 2553a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman uint64_t IntSize = getTypeSize(IntTy); 2554a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); 2555a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; 2556a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman} 2557a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 25587cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getIntegerTypeOrder - Returns the highest ranked integer type: 25597cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 25607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1. 25617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { 2562f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Type *LHSC = getCanonicalType(LHS).getTypePtr(); 2563f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner Type *RHSC = getCanonicalType(RHS).getTypePtr(); 25647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSC == RHSC) return 0; 25655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2566f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner bool LHSUnsigned = LHSC->isUnsignedIntegerType(); 2567f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner bool RHSUnsigned = RHSC->isUnsignedIntegerType(); 25685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner unsigned LHSRank = getIntegerRank(LHSC); 25707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner unsigned RHSRank = getIntegerRank(RHSC); 25715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. 25737cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSRank == RHSRank) return 0; 25747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return LHSRank > RHSRank ? 1 : -1; 25757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner } 25767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner 25777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. 25787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSUnsigned) { 25797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the unsigned [LHS] type is larger, return it. 25807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (LHSRank >= RHSRank) 25817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return 1; 25827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner 25837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the signed type can represent all values of the unsigned type, it 25847cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // wins. Because we are dealing with 2's complement and types that are 25857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // powers of two larger than each other, this is always safe. 25867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return -1; 25877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner } 25887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner 25897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the unsigned [RHS] type is larger, return it. 25907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner if (RHSRank >= LHSRank) 25917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return -1; 25925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 25937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // If the signed type can represent all values of the unsigned type, it 25947cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // wins. Because we are dealing with 2's complement and types that are 25957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner // powers of two larger than each other, this is always safe. 25967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner return 1; 25975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 259871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 259971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson// getCFConstantStringType - Return the type used for constant CFStrings. 260071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() { 260171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson if (!CFConstantStringTypeDecl) { 26026c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner CFConstantStringTypeDecl = 260339ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 2604df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek &Idents.get("NSConstantString")); 2605f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson QualType FieldTypes[4]; 260671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 260771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson // const int *isa; 260871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); 2609f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson // int flags; 2610f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson FieldTypes[1] = IntTy; 261171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson // const char *str; 2612f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); 261371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson // long length; 2614f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson FieldTypes[3] = LongTy; 261571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 261644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor // Create fields 261744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor for (unsigned i = 0; i < 4; ++i) { 261844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, 261944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor SourceLocation(), 0, 2620a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis FieldTypes[i], /*DInfo=*/0, 2621a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis /*BitWidth=*/0, 26224afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor /*Mutable=*/false); 262317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis CFConstantStringTypeDecl->addDecl(Field); 262444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor } 262544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 262644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringTypeDecl->completeDefinition(*this); 262771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson } 262871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson 262971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson return getTagDeclType(CFConstantStringTypeDecl); 26308467583c2704e7a9691ea56939a029015f0ade0aGabor Greif} 2631b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson 2632319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) { 26336217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RecordType *Rec = T->getAs<RecordType>(); 2634319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor assert(Rec && "Invalid CFConstantStringType"); 2635319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor CFConstantStringTypeDecl = Rec->getDecl(); 2636319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor} 2637319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor 2638bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders CarlssonQualType ASTContext::getObjCFastEnumerationStateType() 2639bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson{ 2640bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson if (!ObjCFastEnumerationStateTypeDecl) { 264144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor ObjCFastEnumerationStateTypeDecl = 264244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), 264344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor &Idents.get("__objcFastEnumerationState")); 264444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 2645bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson QualType FieldTypes[] = { 2646bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson UnsignedLongTy, 2647de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff getPointerType(ObjCIdTypedefType), 2648bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson getPointerType(UnsignedLongTy), 2649bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson getConstantArrayType(UnsignedLongTy, 2650bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson llvm::APInt(32, 5), ArrayType::Normal, 0) 2651bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson }; 2652bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 265344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor for (size_t i = 0; i < 4; ++i) { 265444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *Field = FieldDecl::Create(*this, 265544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor ObjCFastEnumerationStateTypeDecl, 265644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor SourceLocation(), 0, 2657a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis FieldTypes[i], /*DInfo=*/0, 2658a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis /*BitWidth=*/0, 26594afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor /*Mutable=*/false); 266017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis ObjCFastEnumerationStateTypeDecl->addDecl(Field); 266144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor } 2662bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 266344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); 2664bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson } 2665bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 2666bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 2667bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson} 2668bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson 2669319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) { 26706217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RecordType *Rec = T->getAs<RecordType>(); 2671319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor assert(Rec && "Invalid ObjCFAstEnumerationStateType"); 2672319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); 2673319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor} 2674319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor 2675e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL: 2676e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL; 26772d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) { 2678e8c49533521c40643653f943d47229e62d277f88Anders Carlsson if (const TypedefType *TT = dyn_cast<TypedefType>(T)) 2679bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) 2680bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner return II->isStr("BOOL"); 268185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 268285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson return false; 268385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson} 268485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 2685a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding 268633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose. 2687a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) { 268898be4943e8dc4f3905629a7102668960873cf863Chris Lattner uint64_t sz = getTypeSize(type); 268933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 269033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Make all integer and enum types at least as large as an int 269133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian if (sz > 0 && type->isIntegralType()) 269298be4943e8dc4f3905629a7102668960873cf863Chris Lattner sz = std::max(sz, getTypeSize(IntTy)); 269333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Treat arrays as pointers, since that's how they're passed in. 269433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian else if (type->isArrayType()) 269598be4943e8dc4f3905629a7102668960873cf863Chris Lattner sz = getTypeSize(VoidPtrTy); 269698be4943e8dc4f3905629a7102668960873cf863Chris Lattner return sz / getTypeSize(CharTy); 269733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian} 269833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 2699a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method 270033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration. 2701c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, 2702e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner std::string& S) { 2703c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: This is not very efficient. 2704ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian // Encode type qualifer, 'in', 'inout', etc. for the return type. 2705a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); 270633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Encode result type. 27070d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar getObjCEncodingForType(Decl->getResultType(), S); 270833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Compute size of all parameters. 270933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Start with computing size of a pointer in number of bytes. 271033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // FIXME: There might(should) be a better way of doing this computation! 271133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian SourceLocation Loc; 271298be4943e8dc4f3905629a7102668960873cf863Chris Lattner int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); 271333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // The first two arguments (self and _cmd) are pointers; account for 271433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // their size. 271533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian int ParmOffset = 2 * PtrSize; 271689951a86b594513c2a013532ed45d197413b1087Chris Lattner for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 271789951a86b594513c2a013532ed45d197413b1087Chris Lattner E = Decl->param_end(); PI != E; ++PI) { 271889951a86b594513c2a013532ed45d197413b1087Chris Lattner QualType PType = (*PI)->getType(); 271989951a86b594513c2a013532ed45d197413b1087Chris Lattner int sz = getObjCEncodingTypeSize(PType); 2720a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); 272133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian ParmOffset += sz; 272233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian } 272333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += llvm::utostr(ParmOffset); 272433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += "@0:"; 272533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += llvm::utostr(PtrSize); 272633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 272733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // Argument types. 272833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian ParmOffset = 2 * PtrSize; 272989951a86b594513c2a013532ed45d197413b1087Chris Lattner for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), 273089951a86b594513c2a013532ed45d197413b1087Chris Lattner E = Decl->param_end(); PI != E; ++PI) { 273189951a86b594513c2a013532ed45d197413b1087Chris Lattner ParmVarDecl *PVDecl = *PI; 27324306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian QualType PType = PVDecl->getOriginalType(); 27334306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian if (const ArrayType *AT = 2734ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { 2735ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff // Use array's original type only if it has known number of 2736ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff // elements. 2737bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff if (!isa<ConstantArrayType>(AT)) 2738ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff PType = PVDecl->getType(); 2739ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff } else if (PType->isFunctionType()) 2740ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff PType = PVDecl->getType(); 2741ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian // Process argument qualifiers for user supplied arguments; such as, 274233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian // 'in', 'inout', etc. 27434306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); 27440d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar getObjCEncodingForType(PType, S); 274533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian S += llvm::utostr(ParmOffset); 2746a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ParmOffset += getObjCEncodingTypeSize(PType); 274733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian } 274833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian} 274933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian 2750c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this 275183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an 2752c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be 2753c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties. 275483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// Property attributes are stored as a comma-delimited C string. The simple 275583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// attributes readonly and bycopy are encoded as single characters. The 275683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// parametrized attributes, getter=name, setter=name, and ivar=name, are 275783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// encoded as single characters, followed by an identifier. Property types 275883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// are also encoded as a parametrized attribute. The characters used to encode 275983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration: 276083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code 276183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes { 276283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R', // property is read-only. 276383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C', // property is a copy of the value last assigned 276483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&', // property is a reference to the value last assigned 276583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D', // property is dynamic 276683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G', // followed by getter selector name 276783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S', // followed by setter selector name 276883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V' // followed by instance variable name 276983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't' // followed by old-style type encoding. 277083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W' // 'weak' property 277183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P' // property GC'able 277283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N' // property non-atomic 277383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// }; 277483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode 2775c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 2776c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar const Decl *Container, 2777e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner std::string& S) { 2778c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // Collect information from the property implementation decl(s). 2779c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar bool Dynamic = false; 2780c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar ObjCPropertyImplDecl *SynthesizePID = 0; 2781c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2782c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: Duplicated code due to poor abstraction. 2783c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (Container) { 2784c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (const ObjCCategoryImplDecl *CID = 2785c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar dyn_cast<ObjCCategoryImplDecl>(Container)) { 2786c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar for (ObjCCategoryImplDecl::propimpl_iterator 278717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = CID->propimpl_begin(), e = CID->propimpl_end(); 2788653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor i != e; ++i) { 2789c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 2790c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyDecl() == PD) { 2791c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 2792c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar Dynamic = true; 2793c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 2794c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar SynthesizePID = PID; 2795c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2796c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2797c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2798c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 279961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); 2800c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar for (ObjCCategoryImplDecl::propimpl_iterator 280117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = OID->propimpl_begin(), e = OID->propimpl_end(); 2802653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor i != e; ++i) { 2803c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 2804c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyDecl() == PD) { 2805c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { 2806c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar Dynamic = true; 2807c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 2808c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar SynthesizePID = PID; 2809c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2810c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2811c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2812c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2813c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2814c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2815c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: This is not very efficient. 2816c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S = "T"; 2817c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2818c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // Encode result type. 2819090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian // GCC has some special rules regarding encoding of properties which 2820090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian // closely resembles encoding of ivars. 2821153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, 2822090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian true /* outermost type */, 2823090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian true /* encoding for property */); 2824c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2825c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PD->isReadOnly()) { 2826c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",R"; 2827c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } else { 2828c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar switch (PD->getSetterKind()) { 2829c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar case ObjCPropertyDecl::Assign: break; 2830c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar case ObjCPropertyDecl::Copy: S += ",C"; break; 2831c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar case ObjCPropertyDecl::Retain: S += ",&"; break; 2832c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2833c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2834c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2835c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // It really isn't clear at all what this means, since properties 2836c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // are "dynamic by default". 2837c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (Dynamic) 2838c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",D"; 2839c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2840090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) 2841090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian S += ",N"; 2842090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian 2843c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 2844c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",G"; 2845077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner S += PD->getGetterName().getAsString(); 2846c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2847c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2848c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 2849c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",S"; 2850077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner S += PD->getSetterName().getAsString(); 2851c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2852c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2853c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar if (SynthesizePID) { 2854c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); 2855c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar S += ",V"; 285639f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner S += OID->getNameAsString(); 2857c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar } 2858c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2859c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar // FIXME: OBJCGC: weak & strong 2860c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar} 2861c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar 2862a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding - 2863a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// Another legacy compatibility encoding: 32-bit longs are encoded as 2864c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian/// 'l' or 'L' , but not always. For typedefs, we need to use 2865a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer! 2866a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 2867a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { 28688e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump if (isa<TypedefType>(PointeeTy.getTypePtr())) { 2869a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) { 2870c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian if (BT->getKind() == BuiltinType::ULong && 2871c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 2872a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian PointeeTy = UnsignedIntTy; 2873c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian else 2874c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian if (BT->getKind() == BuiltinType::Long && 2875c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) 2876a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian PointeeTy = IntTy; 2877a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2878a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2879a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian} 2880a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian 28817d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S, 2882153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar const FieldDecl *Field) { 288382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // We follow the behavior of gcc, expanding structures which are 288482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // directly pointed to, and expanding embedded structures. Note that 288582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // these rules are sufficient to prevent recursive encoding of the 288682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar // same type. 28875b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian getObjCEncodingForTypeImpl(T, S, true, true, Field, 28885b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian true /* outermost type */); 288982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar} 289082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar 28918b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanianstatic void EncodeBitField(const ASTContext *Context, std::string& S, 2892153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar const FieldDecl *FD) { 28938b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian const Expr *E = FD->getBitWidth(); 28948b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); 28958b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian ASTContext *Ctx = const_cast<ASTContext*>(Context); 28969a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); 28978b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian S += 'b'; 28988b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian S += llvm::utostr(N); 28998b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian} 29008b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian 290182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, 290282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar bool ExpandPointedToStructures, 290382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar bool ExpandStructures, 2904153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar const FieldDecl *FD, 2905090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian bool OutermostType, 29066ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor bool EncodingProperty) { 2907e8c49533521c40643653f943d47229e62d277f88Anders Carlsson if (const BuiltinType *BT = T->getAsBuiltinType()) { 2908ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (FD && FD->isBitField()) 2909ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return EncodeBitField(this, S, FD); 2910ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner char encoding; 2911ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner switch (BT->getKind()) { 2912ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner default: assert(0 && "Unhandled builtin type kind"); 2913ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Void: encoding = 'v'; break; 2914ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Bool: encoding = 'B'; break; 2915ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Char_U: 2916ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UChar: encoding = 'C'; break; 2917ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UShort: encoding = 'S'; break; 2918ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UInt: encoding = 'I'; break; 2919ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::ULong: 292072696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian encoding = 2921ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; 292272696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian break; 2923ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::UInt128: encoding = 'T'; break; 2924ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::ULongLong: encoding = 'Q'; break; 2925ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Char_S: 2926ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::SChar: encoding = 'c'; break; 2927ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Short: encoding = 's'; break; 2928ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Int: encoding = 'i'; break; 2929ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Long: 2930ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner encoding = 2931ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; 2932ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner break; 2933ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::LongLong: encoding = 'q'; break; 2934ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Int128: encoding = 't'; break; 2935ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Float: encoding = 'f'; break; 2936ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::Double: encoding = 'd'; break; 2937ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner case BuiltinType::LongDouble: encoding = 'd'; break; 293843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian } 2939ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 2940ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += encoding; 2941ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 2942ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 2943ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 2944ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (const ComplexType *CT = T->getAsComplexType()) { 2945c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson S += 'j'; 2946c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, 2947c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson false); 2948ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 2949ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 2950ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 29516217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const PointerType *PT = T->getAs<PointerType>()) { 295285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson QualType PointeeTy = PT->getPointeeType(); 2953a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian bool isReadOnly = false; 2954a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // For historical/compatibility reasons, the read-only qualifier of the 2955a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // pointee gets emitted _before_ the '^'. The read-only qualifier of 2956a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // the pointer itself gets ignored, _unless_ we are looking at a typedef! 2957a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // Also, do not emit the 'r' for anything but the outermost type! 29588e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump if (isa<TypedefType>(T.getTypePtr())) { 2959a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (OutermostType && T.isConstQualified()) { 2960a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian isReadOnly = true; 2961a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian S += 'r'; 2962a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 29639fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump } else if (OutermostType) { 2964a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian QualType P = PointeeTy; 29656217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek while (P->getAs<PointerType>()) 29666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek P = P->getAs<PointerType>()->getPointeeType(); 2967a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (P.isConstQualified()) { 2968a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian isReadOnly = true; 2969a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian S += 'r'; 2970a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2971a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2972a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (isReadOnly) { 2973a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // Another legacy compatibility encoding. Some ObjC qualifier and type 2974a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // combinations need to be rearranged. 2975a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian // Rewrite "in const" from "nr" to "rn" 2976a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian const char * s = S.c_str(); 2977a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian int len = S.length(); 2978a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { 2979a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian std::string replace = "rn"; 2980a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian S.replace(S.end()-2, S.end(), replace); 2981a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 2982a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian } 298314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (isObjCSelType(PointeeTy)) { 29848baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson S += ':'; 29858baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson return; 2986c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian } 298785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 298885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson if (PointeeTy->isCharType()) { 298985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson // char pointer types should be encoded as '*' unless it is a 299085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson // type that has been typedef'd to 'BOOL'. 2991e8c49533521c40643653f943d47229e62d277f88Anders Carlsson if (!isTypeTypedefedAsBOOL(PointeeTy)) { 299285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson S += '*'; 299385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson return; 299485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson } 29956217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { 29969533a7fdb8397421f3be52e879442460a87389f6Steve Naroff // GCC binary compat: Need to convert "struct objc_class *" to "#". 29979533a7fdb8397421f3be52e879442460a87389f6Steve Naroff if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { 29989533a7fdb8397421f3be52e879442460a87389f6Steve Naroff S += '#'; 29999533a7fdb8397421f3be52e879442460a87389f6Steve Naroff return; 30009533a7fdb8397421f3be52e879442460a87389f6Steve Naroff } 30019533a7fdb8397421f3be52e879442460a87389f6Steve Naroff // GCC binary compat: Need to convert "struct objc_object *" to "@". 30029533a7fdb8397421f3be52e879442460a87389f6Steve Naroff if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { 30039533a7fdb8397421f3be52e879442460a87389f6Steve Naroff S += '@'; 30049533a7fdb8397421f3be52e879442460a87389f6Steve Naroff return; 30059533a7fdb8397421f3be52e879442460a87389f6Steve Naroff } 30069533a7fdb8397421f3be52e879442460a87389f6Steve Naroff // fall through... 300785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson } 300885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson S += '^'; 3009a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian getLegacyIntegralTypeEncoding(PointeeTy); 3010a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian 3011ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, 301243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian NULL); 3013ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3014ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3015ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3016ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (const ArrayType *AT = 3017ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // Ignore type qualifiers etc. 3018ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { 3019559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson if (isa<IncompleteArrayType>(AT)) { 3020559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson // Incomplete arrays are encoded as a pointer to the array element. 3021559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += '^'; 3022559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson 3023559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson getObjCEncodingForTypeImpl(AT->getElementType(), S, 3024559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson false, ExpandStructures, FD); 3025559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson } else { 3026559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += '['; 302785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 3028559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) 3029559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += llvm::utostr(CAT->getSize().getZExtValue()); 3030559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson else { 3031559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson //Variable length arrays are encoded as a regular array with 0 elements. 3032559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson assert(isa<VariableArrayType>(AT) && "Unknown array type!"); 3033559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += '0'; 3034559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson } 303585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 3036559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson getObjCEncodingForTypeImpl(AT->getElementType(), S, 3037559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson false, ExpandStructures, FD); 3038559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson S += ']'; 3039559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson } 3040ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3041ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3042ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3043ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->getAsFunctionType()) { 3044c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson S += '?'; 3045ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3046ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3047ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 30486217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const RecordType *RTy = T->getAs<RecordType>()) { 304982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar RecordDecl *RDecl = RTy->getDecl(); 3050d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += RDecl->isUnion() ? '(' : '{'; 3051502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar // Anonymous structures print as '?' 3052502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar if (const IdentifierInfo *II = RDecl->getIdentifier()) { 3053502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar S += II->getName(); 3054502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar } else { 3055502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar S += '?'; 3056502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar } 30570d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar if (ExpandStructures) { 30587d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian S += '='; 305917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::field_iterator Field = RDecl->field_begin(), 306017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis FieldEnd = RDecl->field_end(); 306144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor Field != FieldEnd; ++Field) { 306243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian if (FD) { 3063d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += '"'; 306444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor S += Field->getNameAsString(); 3065d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += '"'; 3066d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar } 3067d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar 3068d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar // Special case bit-fields. 306943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian if (Field->isBitField()) { 307043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian getObjCEncodingForTypeImpl(Field->getType(), S, false, true, 307143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian (*Field)); 3072d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar } else { 3073a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian QualType qt = Field->getType(); 3074a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian getLegacyIntegralTypeEncoding(qt); 3075a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian getObjCEncodingForTypeImpl(qt, S, false, true, 307643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian FD); 3077d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar } 30787d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian } 30796de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian } 3080d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar S += RDecl->isUnion() ? ')' : '}'; 3081ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3082ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3083ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3084ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->isEnumeralType()) { 30858b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian if (FD && FD->isBitField()) 30868b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian EncodeBitField(this, S, FD); 30878b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian else 30888b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian S += 'i'; 3089ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3090ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3091ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3092ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->isBlockPointerType()) { 309321a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff S += "@?"; // Unlike a pointer-to-function, which is "^?". 3094ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3095ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3096ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3097ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (T->isObjCInterfaceType()) { 309843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian // @encode(class_name) 309943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl(); 310043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += '{'; 310143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian const IdentifierInfo *II = OI->getIdentifier(); 310243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += II->getName(); 310343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += '='; 3104f1690858344968358131f8d5690d9ee458883000Chris Lattner llvm::SmallVector<FieldDecl*, 32> RecFields; 310543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian CollectObjCIvars(OI, RecFields); 3106f1690858344968358131f8d5690d9ee458883000Chris Lattner for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { 310743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian if (RecFields[i]->isBitField()) 310843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 310943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian RecFields[i]); 311043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian else 311143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, 311243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian FD); 311343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian } 311443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian S += '}'; 3115ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 311643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian } 3117ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3118ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) { 311914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (OPT->isObjCIdType()) { 312014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '@'; 312114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 3122ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3123ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3124ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (OPT->isObjCClassType()) { 312514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '#'; 312614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 3127ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3128ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3129ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (OPT->isObjCQualifiedIdType()) { 313014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff getObjCEncodingForTypeImpl(getObjCIdType(), S, 313114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff ExpandPointedToStructures, 313214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff ExpandStructures, FD); 313314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (FD || EncodingProperty) { 313414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff // Note that we do extended encoding of protocol qualifer list 313514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff // Only when doing ivar or property encoding. 313614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '"'; 313767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 313867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff E = OPT->qual_end(); I != E; ++I) { 313914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '<'; 314014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += (*I)->getNameAsString(); 314114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '>'; 314214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 314314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff S += '"'; 314414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 314514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 3146ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3147ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3148ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner QualType PointeeTy = OPT->getPointeeType(); 3149ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (!EncodingProperty && 3150ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner isa<TypedefType>(PointeeTy.getTypePtr())) { 3151ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // Another historical/compatibility reason. 3152ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // We encode the underlying type which comes out as 3153ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner // {...}; 3154ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '^'; 3155ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner getObjCEncodingForTypeImpl(PointeeTy, S, 3156ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner false, ExpandPointedToStructures, 3157ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner NULL); 315814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return; 315914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 3160ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3161ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '@'; 3162ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner if (FD || EncodingProperty) { 3163ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '"'; 316467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff S += OPT->getInterfaceDecl()->getNameAsCString(); 316567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), 316667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff E = OPT->qual_end(); I != E; ++I) { 3167ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '<'; 3168ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += (*I)->getNameAsString(); 3169ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '>'; 3170ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3171ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner S += '"'; 3172ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3173ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner return; 3174ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner } 3175ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner 3176ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner assert(0 && "@encode for type not implemented!"); 317785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson} 317885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson 3179a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 3180ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian std::string& S) const { 3181ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_In) 3182ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'n'; 3183ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Inout) 3184ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'N'; 3185ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Out) 3186ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'o'; 3187ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Bycopy) 3188ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'O'; 3189ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Byref) 3190ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'R'; 3191ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian if (QT & Decl::OBJC_TQ_Oneway) 3192ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian S += 'V'; 3193ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian} 3194ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian 3195ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) { 3196b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); 3197b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson 3198b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson BuiltinVaListType = T; 3199b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson} 3200b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson 3201ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) { 3202de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff ObjCIdTypedefType = T; 32037e219e47de26346885d667131977bd9ca2d7662aSteve Naroff} 32047e219e47de26346885d667131977bd9ca2d7662aSteve Naroff 3205ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) { 3206319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor ObjCSelType = T; 3207319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor 3208319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor const TypedefType *TT = T->getAsTypedefType(); 3209319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor if (!TT) 3210319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor return; 3211319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor TypedefDecl *TD = TT->getDecl(); 3212b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian 3213b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian // typedef struct objc_selector *SEL; 32146217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>(); 3215c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian if (!ptr) 3216c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian return; 3217b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); 3218c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian if (!rec) 3219c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian return; 3220b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian SelStructType = rec; 3221b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian} 3222b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian 3223ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) { 3224a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCProtoType = QT; 3225390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian} 3226390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian 3227ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) { 3228de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff ObjCClassTypedefType = T; 32298baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson} 32308baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson 3231a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { 3232a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek assert(ObjCConstantStringType.isNull() && 32332198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff "'NSConstantString' type already set!"); 32342198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff 3235a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek ObjCConstantStringType = getObjCInterfaceType(Decl); 32362198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff} 32372198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff 32387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a qualified 32397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c std::vector. 32407532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 32417532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor bool TemplateKeyword, 32427532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor TemplateDecl *Template) { 32437532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor llvm::FoldingSetNodeID ID; 32447532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); 32457532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32467532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor void *InsertPos = 0; 32477532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateName *QTN = 32487532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 32497532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor if (!QTN) { 32507532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); 32517532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QualifiedTemplateNames.InsertNode(QTN, InsertPos); 32527532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor } 32537532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32547532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor return TemplateName(QTN); 32557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor} 32567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 3257d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified 3258d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector. 3259d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, 3260d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor bool TemplateKeyword, 3261d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor OverloadedFunctionDecl *Template) { 3262d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor llvm::FoldingSetNodeID ID; 3263d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); 3264d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 3265d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor void *InsertPos = 0; 3266d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateName *QTN = 3267d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 3268d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor if (!QTN) { 3269d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); 3270d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor QualifiedTemplateNames.InsertNode(QTN, InsertPos); 3271d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor } 3272d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 3273d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor return TemplateName(QTN); 3274d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor} 3275d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor 32767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent 32777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply. 32787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, 32797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor const IdentifierInfo *Name) { 32807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor assert(NNS->isDependent() && "Nested name specifier must be dependent"); 32817532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor llvm::FoldingSetNodeID ID; 32837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateName::Profile(ID, NNS, Name); 32847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32857532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor void *InsertPos = 0; 32867532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateName *QTN = 32877532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); 32887532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32897532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor if (QTN) 32907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor return TemplateName(QTN); 32917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 32927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); 32937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor if (CanonNNS == NNS) { 32947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QTN = new (*this,4) DependentTemplateName(NNS, Name); 32957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor } else { 32967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor TemplateName Canon = getDependentTemplateName(CanonNNS, Name); 32977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); 32987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor } 32997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 33007532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor DependentTemplateNames.InsertNode(QTN, InsertPos); 33017532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor return TemplateName(QTN); 33027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor} 33037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 3304b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by 3305d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type 3306d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType. 3307d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas GregorQualType ASTContext::getFromTargetType(unsigned Type) const { 3308b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor switch (Type) { 3309b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::NoInt: return QualType(); 3310b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedShort: return ShortTy; 3311b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedShort: return UnsignedShortTy; 3312b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedInt: return IntTy; 3313b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedInt: return UnsignedIntTy; 3314b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedLong: return LongTy; 3315b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedLong: return UnsignedLongTy; 3316b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::SignedLongLong: return LongLongTy; 3317b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; 3318b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor } 3319b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor 3320b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor assert(false && "Unhandled TargetInfo::IntType value"); 3321b3ac5434ab936f092b8cc48349cb01db3a1e1c76Daniel Dunbar return QualType(); 3322b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor} 3323b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek 3324b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===// 3325b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek// Type Predicates. 3326b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===// 3327b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek 3328fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using 3329fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type. 3330fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types. 3331f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type. 3332fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// 3333fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const { 3334fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { 3335fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian if (TypedefDecl *TD = TDT->getDecl()) 333640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TD->getAttr<ObjCNSObjectAttr>()) 3337fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian return true; 3338fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian } 3339fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian return false; 3340fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian} 3341fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian 33424fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 33434fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute. 33444fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// 33454fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz JahanianQualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const { 3346b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner QualType::GCAttrTypes GCAttrs = QualType::GCNone; 33474fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian if (getLangOptions().ObjC1 && 33484fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian getLangOptions().getGCMode() != LangOptions::NonGC) { 3349b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner GCAttrs = Ty.getObjCGCAttr(); 33504fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian // Default behavious under objective-c's gc is for objective-c pointers 3351a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian // (or pointers to them) be treated as though they were declared 3352a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian // as __strong. 3353a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian if (GCAttrs == QualType::GCNone) { 3354f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff if (Ty->isObjCObjectPointerType()) 3355a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian GCAttrs = QualType::Strong; 3356a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian else if (Ty->isPointerType()) 33576217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); 3358a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian } 3359c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian // Non-pointers have none gc'able attribute regardless of the attribute 3360c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian // set on them. 3361f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) 3362c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian return QualType::GCNone; 33634fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian } 3364b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner return GCAttrs; 33654fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian} 33664fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian 33676ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===// 33686ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner// Type Compatibility Testing 33696ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===// 3370770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner 33716ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// areCompatVectorTypes - Return true if the two specified vector types are 33726ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible. 33736ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS, 33746ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner const VectorType *RHS) { 33756ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner assert(LHS->isCanonical() && RHS->isCanonical()); 33766ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return LHS->getElementType() == RHS->getElementType() && 337761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner LHS->getNumElements() == RHS->getNumElements(); 33786ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner} 33796ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 33804084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===// 33814084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. 33824084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===// 33834084c306635b70f37029dca938444e6013f08684Steve Naroff 33844084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the 33854084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'. 33860fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 33870fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian ObjCProtocolDecl *rProto) { 33884084c306635b70f37029dca938444e6013f08684Steve Naroff if (lProto == rProto) 338914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return true; 33904084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), 33914084c306635b70f37029dca938444e6013f08684Steve Naroff E = rProto->protocol_end(); PI != E; ++PI) 33924084c306635b70f37029dca938444e6013f08684Steve Naroff if (ProtocolCompatibleWithProtocol(lProto, *PI)) 33934084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 33944084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 33954084c306635b70f37029dca938444e6013f08684Steve Naroff} 339614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 33974084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> 33984084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false 33994084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise. 34004084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { 34014084c306635b70f37029dca938444e6013f08684Steve Naroff if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) 34024084c306635b70f37029dca938444e6013f08684Steve Naroff return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); 34034084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 34044084c306635b70f37029dca938444e6013f08684Steve Naroff} 34054084c306635b70f37029dca938444e6013f08684Steve Naroff 34064084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an 34074084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType. 34084084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, 34094084c306635b70f37029dca938444e6013f08684Steve Naroff bool compare) { 34104084c306635b70f37029dca938444e6013f08684Steve Naroff // Allow id<P..> and an 'id' or void* type in all cases. 34114084c306635b70f37029dca938444e6013f08684Steve Naroff if (lhs->isVoidPointerType() || 34124084c306635b70f37029dca938444e6013f08684Steve Naroff lhs->isObjCIdType() || lhs->isObjCClassType()) 34134084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 34144084c306635b70f37029dca938444e6013f08684Steve Naroff else if (rhs->isVoidPointerType() || 34154084c306635b70f37029dca938444e6013f08684Steve Naroff rhs->isObjCIdType() || rhs->isObjCClassType()) 34164084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 34174084c306635b70f37029dca938444e6013f08684Steve Naroff 34184084c306635b70f37029dca938444e6013f08684Steve Naroff if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { 34194084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType(); 34204084c306635b70f37029dca938444e6013f08684Steve Naroff 34214084c306635b70f37029dca938444e6013f08684Steve Naroff if (!rhsOPT) return false; 34224084c306635b70f37029dca938444e6013f08684Steve Naroff 34234084c306635b70f37029dca938444e6013f08684Steve Naroff if (rhsOPT->qual_empty()) { 34244084c306635b70f37029dca938444e6013f08684Steve Naroff // If the RHS is a unqualified interface pointer "NSString*", 34254084c306635b70f37029dca938444e6013f08684Steve Naroff // make sure we check the class hierarchy. 34264084c306635b70f37029dca938444e6013f08684Steve Naroff if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { 34274084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 34284084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsQID->qual_end(); I != E; ++I) { 34294084c306635b70f37029dca938444e6013f08684Steve Naroff // when comparing an id<P> on lhs with a static type on rhs, 34304084c306635b70f37029dca938444e6013f08684Steve Naroff // see if static class implements all of id's protocols, directly or 34314084c306635b70f37029dca938444e6013f08684Steve Naroff // through its super class and categories. 34320fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian if (!rhsID->ClassImplementsProtocol(*I, true)) 34334084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 34344084c306635b70f37029dca938444e6013f08684Steve Naroff } 34354084c306635b70f37029dca938444e6013f08684Steve Naroff } 34364084c306635b70f37029dca938444e6013f08684Steve Naroff // If there are no qualifiers and no interface, we have an 'id'. 34374084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 34384084c306635b70f37029dca938444e6013f08684Steve Naroff } 3439de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // Both the right and left sides have qualifiers. 34404084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 34414084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsQID->qual_end(); I != E; ++I) { 34424084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *lhsProto = *I; 34434084c306635b70f37029dca938444e6013f08684Steve Naroff bool match = false; 3444de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff 3445de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // when comparing an id<P> on lhs with a static type on rhs, 3446de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // see if static class implements all of id's protocols, directly or 3447de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff // through its super class and categories. 34484084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), 34494084c306635b70f37029dca938444e6013f08684Steve Naroff E = rhsOPT->qual_end(); J != E; ++J) { 34504084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *rhsProto = *J; 34514084c306635b70f37029dca938444e6013f08684Steve Naroff if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || 34524084c306635b70f37029dca938444e6013f08684Steve Naroff (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { 34534084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 34548f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff break; 34558f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff } 3456de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff } 34574084c306635b70f37029dca938444e6013f08684Steve Naroff // If the RHS is a qualified interface pointer "NSString<P>*", 34584084c306635b70f37029dca938444e6013f08684Steve Naroff // make sure we check the class hierarchy. 34594084c306635b70f37029dca938444e6013f08684Steve Naroff if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { 34604084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), 34614084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsQID->qual_end(); I != E; ++I) { 34624084c306635b70f37029dca938444e6013f08684Steve Naroff // when comparing an id<P> on lhs with a static type on rhs, 34634084c306635b70f37029dca938444e6013f08684Steve Naroff // see if static class implements all of id's protocols, directly or 34644084c306635b70f37029dca938444e6013f08684Steve Naroff // through its super class and categories. 34650fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian if (rhsID->ClassImplementsProtocol(*I, true)) { 34664084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 34674084c306635b70f37029dca938444e6013f08684Steve Naroff break; 34684084c306635b70f37029dca938444e6013f08684Steve Naroff } 34694084c306635b70f37029dca938444e6013f08684Steve Naroff } 34704084c306635b70f37029dca938444e6013f08684Steve Naroff } 34714084c306635b70f37029dca938444e6013f08684Steve Naroff if (!match) 3472de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff return false; 3473de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff } 34744084c306635b70f37029dca938444e6013f08684Steve Naroff 3475de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff return true; 3476de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff } 34774084c306635b70f37029dca938444e6013f08684Steve Naroff 34784084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); 34794084c306635b70f37029dca938444e6013f08684Steve Naroff assert(rhsQID && "One of the LHS/RHS should be id<x>"); 34804084c306635b70f37029dca938444e6013f08684Steve Naroff 34814084c306635b70f37029dca938444e6013f08684Steve Naroff if (const ObjCObjectPointerType *lhsOPT = 34824084c306635b70f37029dca938444e6013f08684Steve Naroff lhs->getAsObjCInterfacePointerType()) { 34834084c306635b70f37029dca938444e6013f08684Steve Naroff if (lhsOPT->qual_empty()) { 34844084c306635b70f37029dca938444e6013f08684Steve Naroff bool match = false; 34854084c306635b70f37029dca938444e6013f08684Steve Naroff if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { 34864084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), 34874084c306635b70f37029dca938444e6013f08684Steve Naroff E = rhsQID->qual_end(); I != E; ++I) { 34884084c306635b70f37029dca938444e6013f08684Steve Naroff // when comparing an id<P> on lhs with a static type on rhs, 34894084c306635b70f37029dca938444e6013f08684Steve Naroff // see if static class implements all of id's protocols, directly or 34904084c306635b70f37029dca938444e6013f08684Steve Naroff // through its super class and categories. 34910fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian if (lhsID->ClassImplementsProtocol(*I, true)) { 34924084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 34934084c306635b70f37029dca938444e6013f08684Steve Naroff break; 34944084c306635b70f37029dca938444e6013f08684Steve Naroff } 34954084c306635b70f37029dca938444e6013f08684Steve Naroff } 34964084c306635b70f37029dca938444e6013f08684Steve Naroff if (!match) 34974084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 34984084c306635b70f37029dca938444e6013f08684Steve Naroff } 34994084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 35004084c306635b70f37029dca938444e6013f08684Steve Naroff } 35014084c306635b70f37029dca938444e6013f08684Steve Naroff // Both the right and left sides have qualifiers. 35024084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), 35034084c306635b70f37029dca938444e6013f08684Steve Naroff E = lhsOPT->qual_end(); I != E; ++I) { 35044084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *lhsProto = *I; 35054084c306635b70f37029dca938444e6013f08684Steve Naroff bool match = false; 35064084c306635b70f37029dca938444e6013f08684Steve Naroff 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. 35104084c306635b70f37029dca938444e6013f08684Steve Naroff for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), 35114084c306635b70f37029dca938444e6013f08684Steve Naroff E = rhsQID->qual_end(); J != E; ++J) { 35124084c306635b70f37029dca938444e6013f08684Steve Naroff ObjCProtocolDecl *rhsProto = *J; 35134084c306635b70f37029dca938444e6013f08684Steve Naroff if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || 35144084c306635b70f37029dca938444e6013f08684Steve Naroff (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { 35154084c306635b70f37029dca938444e6013f08684Steve Naroff match = true; 35164084c306635b70f37029dca938444e6013f08684Steve Naroff break; 35174084c306635b70f37029dca938444e6013f08684Steve Naroff } 35184084c306635b70f37029dca938444e6013f08684Steve Naroff } 35194084c306635b70f37029dca938444e6013f08684Steve Naroff if (!match) 35204084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 35214084c306635b70f37029dca938444e6013f08684Steve Naroff } 35224084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 35234084c306635b70f37029dca938444e6013f08684Steve Naroff } 35244084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 35254084c306635b70f37029dca938444e6013f08684Steve Naroff} 35264084c306635b70f37029dca938444e6013f08684Steve Naroff 35274084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are 35284084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS. This handles validation of any 35294084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS. 35304084c306635b70f37029dca938444e6013f08684Steve Naroff/// 35314084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 35324084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCObjectPointerType *RHSOPT) { 35334084c306635b70f37029dca938444e6013f08684Steve Naroff // If either type represents the built-in 'id' or 'Class' types, return true. 35344084c306635b70f37029dca938444e6013f08684Steve Naroff if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) 35354084c306635b70f37029dca938444e6013f08684Steve Naroff return true; 35364084c306635b70f37029dca938444e6013f08684Steve Naroff 35374084c306635b70f37029dca938444e6013f08684Steve Naroff if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) 35384084c306635b70f37029dca938444e6013f08684Steve Naroff return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), 35394084c306635b70f37029dca938444e6013f08684Steve Naroff QualType(RHSOPT,0), 35404084c306635b70f37029dca938444e6013f08684Steve Naroff false); 35414084c306635b70f37029dca938444e6013f08684Steve Naroff 35424084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); 35434084c306635b70f37029dca938444e6013f08684Steve Naroff const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); 35444084c306635b70f37029dca938444e6013f08684Steve Naroff if (LHS && RHS) // We have 2 user-defined types. 35454084c306635b70f37029dca938444e6013f08684Steve Naroff return canAssignObjCInterfaces(LHS, RHS); 35464084c306635b70f37029dca938444e6013f08684Steve Naroff 35474084c306635b70f37029dca938444e6013f08684Steve Naroff return false; 354814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff} 354914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 35503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 35513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const ObjCInterfaceType *RHS) { 35526ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // Verify that the base decls are compatible: the RHS must be a subclass of 35536ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // the LHS. 35546ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) 35556ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return false; 35566ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 35576ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // RHS must have a superset of the protocols in the LHS. If the LHS is not 35586ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // protocol qualified at all, then we are good. 3559c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (LHS->getNumProtocols() == 0) 35606ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return true; 35616ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 35626ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it 35636ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner // isn't a superset. 3564c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff if (RHS->getNumProtocols() == 0) 35656ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner return true; // FIXME: should return false! 35666ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 3567c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), 3568c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff LHSPE = LHS->qual_end(); 356991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff LHSPI != LHSPE; LHSPI++) { 357091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff bool RHSImplementsProtocol = false; 357191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff 357291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // If the RHS doesn't implement the protocol on the left, the types 357391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // are incompatible. 3574c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), 35754084c306635b70f37029dca938444e6013f08684Steve Naroff RHSPE = RHS->qual_end(); 35768f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff RHSPI != RHSPE; RHSPI++) { 35778f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { 357891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff RHSImplementsProtocol = true; 35798f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff break; 35808f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff } 358191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff } 358291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // FIXME: For better diagnostics, consider passing back the protocol name. 358391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff if (!RHSImplementsProtocol) 358491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff return false; 358591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff } 358691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff // The RHS implements all protocols listed on the LHS. 358791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff return true; 35886ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner} 35896ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner 3590389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { 3591389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff // get the "pointed to" types 359214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType(); 359314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType(); 3594389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff 359514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (!LHSOPT || !RHSOPT) 3596389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff return false; 359714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 359814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return canAssignObjCInterfaces(LHSOPT, RHSOPT) || 359914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff canAssignObjCInterfaces(RHSOPT, LHSOPT); 3600389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff} 3601389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff 3602ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, 3603ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type. 3604ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// C99 6.2.7p1: Two types have compatible types if their types are the 3605ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules. 36063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { 36073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return !mergeTypes(LHS, RHS).isNull(); 36083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman} 36093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36103d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { 36113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const FunctionType *lbase = lhs->getAsFunctionType(); 36123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const FunctionType *rbase = rhs->getAsFunctionType(); 361372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); 361472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); 36153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman bool allLTypes = true; 36163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman bool allRTypes = true; 36173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Check return type 36193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); 36203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (retType.isNull()) return QualType(); 362161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) 362261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allLTypes = false; 362361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) 362461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allRTypes = false; 36256dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump // FIXME: double check this 36262455636163fdd18581d7fdae816433f886d88213Mike Stump bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); 36272455636163fdd18581d7fdae816433f886d88213Mike Stump if (NoReturn != lbase->getNoReturnAttr()) 36282455636163fdd18581d7fdae816433f886d88213Mike Stump allLTypes = false; 36292455636163fdd18581d7fdae816433f886d88213Mike Stump if (NoReturn != rbase->getNoReturnAttr()) 36302455636163fdd18581d7fdae816433f886d88213Mike Stump allRTypes = false; 36312455636163fdd18581d7fdae816433f886d88213Mike Stump 36323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto && rproto) { // two C99 style function prototypes 3633465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && 3634465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl "C++ shouldn't be here"); 36353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman unsigned lproto_nargs = lproto->getNumArgs(); 36363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman unsigned rproto_nargs = rproto->getNumArgs(); 36373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Compatible functions must have the same number of arguments 36393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto_nargs != rproto_nargs) 36403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 36413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Variadic and non-variadic functions aren't compatible 36433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto->isVariadic() != rproto->isVariadic()) 36443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 36453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36467fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis if (lproto->getTypeQuals() != rproto->getTypeQuals()) 36477fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis return QualType(); 36487fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis 36493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Check argument compatibility 36503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman llvm::SmallVector<QualType, 10> types; 36513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman for (unsigned i = 0; i < lproto_nargs; i++) { 36523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType largtype = lproto->getArgType(i).getUnqualifiedType(); 36533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); 36543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType argtype = mergeTypes(largtype, rargtype); 36553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (argtype.isNull()) return QualType(); 36563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman types.push_back(argtype); 365761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(argtype) != getCanonicalType(largtype)) 365861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allLTypes = false; 365961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (getCanonicalType(argtype) != getCanonicalType(rargtype)) 366061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner allRTypes = false; 36613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 36623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allLTypes) return lhs; 36633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allRTypes) return rhs; 36643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return getFunctionType(retType, types.begin(), types.size(), 36652455636163fdd18581d7fdae816433f886d88213Mike Stump lproto->isVariadic(), lproto->getTypeQuals(), 36662455636163fdd18581d7fdae816433f886d88213Mike Stump NoReturn); 36673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 36683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (lproto) allRTypes = false; 36703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (rproto) allLTypes = false; 36713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 367272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor const FunctionProtoType *proto = lproto ? lproto : rproto; 36733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (proto) { 3674465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); 36753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (proto->isVariadic()) return QualType(); 36763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Check that the types are compatible with the types that 36773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // would result from default argument promotions (C99 6.7.5.3p15). 36783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // The only types actually affected are promotable integer 36793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // types and floats, which would be passed as a different 36803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // type depending on whether the prototype is visible. 36813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman unsigned proto_nargs = proto->getNumArgs(); 36823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman for (unsigned i = 0; i < proto_nargs; ++i) { 36833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType argTy = proto->getArgType(i); 36843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (argTy->isPromotableIntegerType() || 36853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman getCanonicalType(argTy).getUnqualifiedType() == FloatTy) 36863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 36873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 36883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allLTypes) return lhs; 36903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allRTypes) return rhs; 36913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return getFunctionType(retType, proto->arg_type_begin(), 36922d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump proto->getNumArgs(), proto->isVariadic(), 36932d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump proto->getTypeQuals(), NoReturn); 36943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 36953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 36963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allLTypes) return lhs; 36973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (allRTypes) return rhs; 36982455636163fdd18581d7fdae816433f886d88213Mike Stump return getFunctionNoProtoType(retType, NoReturn); 36993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman} 37003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37013d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { 370243d69750e7f7b26076e7474dec8839bb777b260fBill Wendling // C++ [expr]: If an expression initially has the type "reference to T", the 370343d69750e7f7b26076e7474dec8839bb777b260fBill Wendling // type is adjusted to "T" prior to any further analysis, the expression 370443d69750e7f7b26076e7474dec8839bb777b260fBill Wendling // designates the object or function denoted by the reference, and the 37057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // expression is an lvalue unless the reference is an rvalue reference and 37067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // the expression is a function call (possibly inside parentheses). 37073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // FIXME: C++ shouldn't be going through here! The rules are different 37083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // enough that they should be handled separately. 37097c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* 37107c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl // shouldn't be going through here! 37116217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) 3712c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner LHS = RT->getPointeeType(); 37136217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) 3714c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner RHS = RT->getPointeeType(); 37153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType LHSCan = getCanonicalType(LHS), 37173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman RHSCan = getCanonicalType(RHS); 37183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 3719f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner // If two types are identical, they are compatible. 37203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (LHSCan == RHSCan) 37213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 37223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 37233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // If the qualifiers are different, the types aren't compatible 37245a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // Note that we handle extended qualifiers later, in the 37255a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // case for ExtQualType. 37265a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers()) 37273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 37283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 3729852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman Type::TypeClass LHSClass = LHSCan->getTypeClass(); 3730852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman Type::TypeClass RHSClass = RHSCan->getTypeClass(); 3731f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner 37321adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // We want to consider the two function types to be the same for these 37331adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // comparisons, just force one to the other. 37341adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; 37351adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; 37364c721d381fb279899337d120edd4a24d405e56b2Eli Friedman 373707d258756dc856c6987c394a0972884e6ed46765Eli Friedman // Strip off objc_gc attributes off the top level so they can be merged. 373807d258756dc856c6987c394a0972884e6ed46765Eli Friedman // This is a complete mess, but the attribute itself doesn't make much sense. 3739585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian if (RHSClass == Type::ExtQual) { 374007d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr(); 374107d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (GCAttr != QualType::GCNone) { 374286f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr(); 37438df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian // __weak attribute must appear on both declarations. 374486f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // __strong attribue is redundant if other decl is an objective-c 374586f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // object pointer (or decorated with __strong attribute); otherwise 374686f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // issue error. 374786f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) || 374886f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian (GCAttr == QualType::Strong && GCLHSAttr != GCAttr && 374914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff !LHSCan->isObjCObjectPointerType())) 37508df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian return QualType(); 37518df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian 375207d258756dc856c6987c394a0972884e6ed46765Eli Friedman RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(), 375307d258756dc856c6987c394a0972884e6ed46765Eli Friedman RHS.getCVRQualifiers()); 375407d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType Result = mergeTypes(LHS, RHS); 37558df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (!Result.isNull()) { 37568df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (Result.getObjCGCAttr() == QualType::GCNone) 37578df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = getObjCGCQualType(Result, GCAttr); 37588df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian else if (Result.getObjCGCAttr() != GCAttr) 37598df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = QualType(); 37608df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian } 376107d258756dc856c6987c394a0972884e6ed46765Eli Friedman return Result; 376207d258756dc856c6987c394a0972884e6ed46765Eli Friedman } 3763585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian } 3764585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian if (LHSClass == Type::ExtQual) { 376507d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr(); 376607d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (GCAttr != QualType::GCNone) { 37678df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr(); 37688df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian // __weak attribute must appear on both declarations. __strong 376986f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // __strong attribue is redundant if other decl is an objective-c 377086f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // object pointer (or decorated with __strong attribute); otherwise 377186f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian // issue error. 377286f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) || 377386f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian (GCAttr == QualType::Strong && GCRHSAttr != GCAttr && 377414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff !RHSCan->isObjCObjectPointerType())) 37758df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian return QualType(); 377686f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian 377707d258756dc856c6987c394a0972884e6ed46765Eli Friedman LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(), 377807d258756dc856c6987c394a0972884e6ed46765Eli Friedman LHS.getCVRQualifiers()); 377907d258756dc856c6987c394a0972884e6ed46765Eli Friedman QualType Result = mergeTypes(LHS, RHS); 37808df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (!Result.isNull()) { 37818df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian if (Result.getObjCGCAttr() == QualType::GCNone) 37828df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = getObjCGCQualType(Result, GCAttr); 37838df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian else if (Result.getObjCGCAttr() != GCAttr) 37848df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian Result = QualType(); 37858df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian } 3786354e53da656237e25518b3fafa3bf84de6e6f57eEli Friedman return Result; 378707d258756dc856c6987c394a0972884e6ed46765Eli Friedman } 3788585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian } 3789585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian 37904c721d381fb279899337d120edd4a24d405e56b2Eli Friedman // Same as above for arrays 3791a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) 3792a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner LHSClass = Type::ConstantArray; 3793a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) 3794a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner RHSClass = Type::ConstantArray; 3795a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner 3796213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman // Canonicalize ExtVector -> Vector. 3797213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; 3798213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; 3799ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff 38004e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff // If the canonical type classes don't match. 38014e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff if (LHSClass != RHSClass) { 38021adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, 38031adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner // a signed integer type, or an unsigned integer type. 38043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (const EnumType* ETy = LHS->getAsEnumType()) { 38053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) 38063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return RHS; 3807bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman } 38083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (const EnumType* ETy = RHS->getAsEnumType()) { 38093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) 38103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 3811bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman } 38121adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner 38133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 3814ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff } 38153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 38164a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff // The canonical type classes match. 38171adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner switch (LHSClass) { 381872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base) 381972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 382072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 382172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class: 382272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def" 382372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor assert(false && "Non-canonical and dependent types shouldn't get here"); 382472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 382572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 38267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::LValueReference: 38277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::RValueReference: 382872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::MemberPointer: 382972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor assert(false && "C++ should never be in mergeTypes"); 383072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 383172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 383272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::IncompleteArray: 383372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::VariableArray: 383472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::FunctionProto: 383572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::ExtVector: 383672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor assert(false && "Types are eliminated above"); 383772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 383872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 38391adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::Pointer: 38403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman { 38413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // Merge two pointer types, while trying to preserve typedef info 38426217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); 38436217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); 38443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 38453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ResultType.isNull()) return QualType(); 384607d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 384761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return LHS; 384807d258756dc856c6987c394a0972884e6ed46765Eli Friedman if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 384961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return RHS; 38503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return getPointerType(ResultType); 38513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 3852c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff case Type::BlockPointer: 3853c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff { 3854c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff // Merge two block pointer types, while trying to preserve typedef info 38556217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); 38566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); 3857c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff QualType ResultType = mergeTypes(LHSPointee, RHSPointee); 3858c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff if (ResultType.isNull()) return QualType(); 3859c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) 3860c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff return LHS; 3861c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) 3862c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff return RHS; 3863c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff return getBlockPointerType(ResultType); 3864c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff } 38651adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::ConstantArray: 38663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman { 38673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); 38683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); 38693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) 38703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 38713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman 38723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType LHSElem = getAsArrayType(LHS)->getElementType(); 38733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType RHSElem = getAsArrayType(RHS)->getElementType(); 38743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman QualType ResultType = mergeTypes(LHSElem, RHSElem); 38753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (ResultType.isNull()) return QualType(); 387661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 387761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return LHS; 387861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 387961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return RHS; 38803bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), 38813bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman ArrayType::ArraySizeModifier(), 0); 38823bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), 38833bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman ArrayType::ArraySizeModifier(), 0); 38843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const VariableArrayType* LVAT = getAsVariableArrayType(LHS); 38853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman const VariableArrayType* RVAT = getAsVariableArrayType(RHS); 388661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) 388761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return LHS; 388861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) 388961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return RHS; 38903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (LVAT) { 38913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // FIXME: This isn't correct! But tricky to implement because 38923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // the array's size has to be the size of LHS, but the type 38933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // has to be different. 38943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 38953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 38963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (RVAT) { 38973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // FIXME: This isn't correct! But tricky to implement because 38983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // the array's size has to be the size of RHS, but the type 38993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman // has to be different. 39003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return RHS; 39013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 39023bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; 39033bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; 39047e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor return getIncompleteArrayType(ResultType, 39057e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor ArrayType::ArraySizeModifier(), 0); 39063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman } 39071adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::FunctionNoProto: 39083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return mergeFunctionTypes(LHS, RHS); 390972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Record: 391072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor case Type::Enum: 39113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 39121adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner case Type::Builtin: 39133cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner // Only exactly equal builtin types are compatible, which is tested above. 39143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 391564cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar case Type::Complex: 391664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar // Distinct complex types are incompatible. 391764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar return QualType(); 39183cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner case Type::Vector: 39195a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: The merged type should be an ExtVector! 39203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) 39213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return LHS; 392261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner return QualType(); 392361490e9a965cfee8a78c12c6802138844f04250dCedric Venet case Type::ObjCInterface: { 39245fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff // Check if the interfaces are assignment compatible. 39255a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: This should be type compatibility, e.g. whether 39265a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // "LHS x; RHS x;" at global scope is legal. 39275fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); 39285fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); 39295fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff if (LHSIface && RHSIface && 39305fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff canAssignObjCInterfaces(LHSIface, RHSIface)) 39315fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff return LHS; 39325fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff 39333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman return QualType(); 393461490e9a965cfee8a78c12c6802138844f04250dCedric Venet } 393514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff case Type::ObjCObjectPointer: { 393614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(), 393714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff RHS->getAsObjCObjectPointerType())) 393814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff return LHS; 393914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff 3940bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff return QualType(); 394114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 39425a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman case Type::FixedWidthInt: 39435a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // Distinct fixed-width integers are not compatible. 39445a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return QualType(); 39455a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman case Type::ExtQual: 39465a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // FIXME: ExtQual types can be compatible even if they're not 39475a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // identical! 39485a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return QualType(); 39495a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // First attempt at an implementation, but I'm not really sure it's 39505a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman // right... 39515a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#if 0 39525a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ExtQualType* LQual = cast<ExtQualType>(LHSCan); 39535a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ExtQualType* RQual = cast<ExtQualType>(RHSCan); 39545a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (LQual->getAddressSpace() != RQual->getAddressSpace() || 39555a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman LQual->getObjCGCAttr() != RQual->getObjCGCAttr()) 39565a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return QualType(); 39575a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman QualType LHSBase, RHSBase, ResultType, ResCanUnqual; 39585a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman LHSBase = QualType(LQual->getBaseType(), 0); 39595a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman RHSBase = QualType(RQual->getBaseType(), 0); 39605a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType = mergeTypes(LHSBase, RHSBase); 39615a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (ResultType.isNull()) return QualType(); 39625a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType(); 39635a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (LHSCan.getUnqualifiedType() == ResCanUnqual) 39645a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return LHS; 39655a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman if (RHSCan.getUnqualifiedType() == ResCanUnqual) 39665a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return RHS; 39675a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace()); 39685a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr()); 39695a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers()); 39705a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman return ResultType; 39715a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#endif 39727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor 39737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor case Type::TemplateSpecialization: 39747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor assert(false && "Dependent types have no size"); 39757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor break; 3976ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff } 397772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 397872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor return QualType(); 3979ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff} 39807192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek 39815426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===// 3982ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman// Integer Predicates 3983ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===// 398488054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner 3985ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) { 3986ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman if (T == BoolTy) 3987ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return 1; 3988f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { 3989f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman return FWIT->getWidth(); 3990f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman } 3991f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman // For builtin types, just use the standard type sizing method 3992ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return (unsigned)getTypeSize(T); 3993ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman} 3994ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman 3995ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) { 3996ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman assert(T->isSignedIntegerType() && "Unexpected type"); 3997ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman if (const EnumType* ETy = T->getAsEnumType()) 3998ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman T = ETy->getDecl()->getIntegerType(); 3999ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman const BuiltinType* BTy = T->getAsBuiltinType(); 4000ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman assert (BTy && "Unexpected signed integer type"); 4001ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman switch (BTy->getKind()) { 4002ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Char_S: 4003ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::SChar: 4004ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedCharTy; 4005ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Short: 4006ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedShortTy; 4007ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Int: 4008ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedIntTy; 4009ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::Long: 4010ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedLongTy; 4011ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman case BuiltinType::LongLong: 4012ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return UnsignedLongLongTy; 40132df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::Int128: 40142df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner return UnsignedInt128Ty; 4015ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman default: 4016ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman assert(0 && "Unexpected signed integer type"); 4017ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman return QualType(); 4018ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman } 4019ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman} 4020ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman 40212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { } 40222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor 40232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { } 402486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 402586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 402686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===// 402786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner// Builtin Type Computation 402886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===// 402986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 403086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the 403186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters. This returns the resultant type. 403286df27bbdbb98c39ec2184695c0561209f91beddChris Lattnerstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, 403386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ASTContext::GetBuiltinTypeError &Error, 403486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner bool AllowTypeModifiers = true) { 403586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Modifiers. 403686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner int HowLong = 0; 403786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner bool Signed = false, Unsigned = false; 403886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 403986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Read the modifiers first. 404086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner bool Done = false; 404186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner while (!Done) { 404286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner switch (*Str++) { 404386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner default: Done = true; --Str; break; 404486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'S': 404586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); 404686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Signed && "Can't use 'S' modifier multiple times!"); 404786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Signed = true; 404886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 404986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'U': 405086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); 405186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Unsigned && "Can't use 'S' modifier multiple times!"); 405286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Unsigned = true; 405386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 405486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'L': 405586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong <= 2 && "Can't have LLLL modifier"); 405686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ++HowLong; 405786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 405886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 405986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 406086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 406186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType Type; 406286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 406386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Read the base type. 406486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner switch (*Str++) { 406586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner default: assert(0 && "Unknown builtin type letter!"); 406686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'v': 406786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && 406886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "Bad modifiers used with 'v'!"); 406986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.VoidTy; 407086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 407186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'f': 407286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && 407386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "Bad modifiers used with 'f'!"); 407486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.FloatTy; 407586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 407686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'd': 407786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong < 2 && !Signed && !Unsigned && 407886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "Bad modifiers used with 'd'!"); 407986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (HowLong) 408086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.LongDoubleTy; 408186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 408286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.DoubleTy; 408386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 408486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 's': 408586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && "Bad modifiers used with 's'!"); 408686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Unsigned) 408786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.UnsignedShortTy; 408886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 408986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.ShortTy; 409086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 409186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'i': 409286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (HowLong == 3) 409386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; 409486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else if (HowLong == 2) 409586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; 409686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else if (HowLong == 1) 409786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; 409886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 409986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; 410086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 410186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'c': 410286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && "Bad modifiers used with 'c'!"); 410386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Signed) 410486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.SignedCharTy; 410586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else if (Unsigned) 410686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.UnsignedCharTy; 410786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner else 410886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.CharTy; 410986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 411086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'b': // boolean 411186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); 411286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.BoolTy; 411386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 411486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'z': // size_t. 411586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); 411686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getSizeType(); 411786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 411886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'F': 411986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getCFConstantStringType(); 412086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 412186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'a': 412286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getBuiltinVaListType(); 412386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Type.isNull() && "builtin va list type not initialized!"); 412486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 412586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'A': 412686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // This is a "reference" to a va_list; however, what exactly 412786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // this means depends on how va_list is defined. There are two 412886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // different kinds of va_list: ones passed by value, and ones 412986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // passed by reference. An example of a by-value va_list is 413086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // x86, where va_list is a char*. An example of by-ref va_list 413186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // is x86-64, where va_list is a __va_list_tag[1]. For x86, 413286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // we want this argument to be a char*&; for x86-64, we want 413386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // it to be a __va_list_tag*. 413486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getBuiltinVaListType(); 413586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(!Type.isNull() && "builtin va list type not initialized!"); 413686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Type->isArrayType()) { 413786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getArrayDecayedType(Type); 413886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } else { 413986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getLValueReferenceType(Type); 414086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 414186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 414286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'V': { 414386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner char *End; 414486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner unsigned NumElements = strtoul(Str, &End, 10); 414586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert(End != Str && "Missing vector size"); 414686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 414786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Str = End; 414886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 414986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); 415086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getVectorType(ElementType, NumElements); 415186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 415286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 41539a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner case 'P': 4154c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor Type = Context.getFILEType(); 4155c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor if (Type.isNull()) { 4156f711c41dd9412a8182793259d355c4f6979ed5edMike Stump Error = ASTContext::GE_Missing_stdio; 415786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return QualType(); 415886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 4159fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump break; 41609a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner case 'J': 4161f711c41dd9412a8182793259d355c4f6979ed5edMike Stump if (Signed) 4162782fa308a765aeac2acb39c4e697c937ec21185bMike Stump Type = Context.getsigjmp_bufType(); 4163f711c41dd9412a8182793259d355c4f6979ed5edMike Stump else 4164f711c41dd9412a8182793259d355c4f6979ed5edMike Stump Type = Context.getjmp_bufType(); 4165f711c41dd9412a8182793259d355c4f6979ed5edMike Stump 4166fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump if (Type.isNull()) { 4167f711c41dd9412a8182793259d355c4f6979ed5edMike Stump Error = ASTContext::GE_Missing_setjmp; 4168fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump return QualType(); 4169fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump } 4170fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump break; 4171782fa308a765aeac2acb39c4e697c937ec21185bMike Stump } 417286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 417386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (!AllowTypeModifiers) 417486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return Type; 417586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 417686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Done = false; 417786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner while (!Done) { 417886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner switch (*Str++) { 417986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner default: Done = true; --Str; break; 418086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case '*': 418186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getPointerType(Type); 418286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 418386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case '&': 418486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Context.getLValueReferenceType(Type); 418586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 418686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // FIXME: There's no way to have a built-in with an rvalue ref arg. 418786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner case 'C': 418886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Type = Type.getQualifiedType(QualType::Const); 418986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner break; 419086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 419186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 419286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 419386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return Type; 419486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner} 419586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 419686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin. 419786df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id, 419886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner GetBuiltinTypeError &Error) { 419986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner const char *TypeStr = BuiltinInfo.GetTypeString(id); 420086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 420186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner llvm::SmallVector<QualType, 8> ArgTypes; 420286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 420386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Error = GE_None; 420486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); 420586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Error != GE_None) 420686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return QualType(); 420786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner while (TypeStr[0] && TypeStr[0] != '.') { 420886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); 420986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Error != GE_None) 421086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return QualType(); 421186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 421286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // Do array -> pointer decay. The builtin should use the decayed type. 421386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (Ty->isArrayType()) 421486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner Ty = getArrayDecayedType(Ty); 421586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 421686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner ArgTypes.push_back(Ty); 421786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner } 421886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 421986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner assert((TypeStr[0] != '.' || TypeStr[1] == 0) && 422086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner "'.' should only occur at end of builtin type list!"); 422186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner 422286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". 422386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner if (ArgTypes.size() == 0 && TypeStr[0] == '.') 422486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return getFunctionNoProtoType(ResType); 422586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), 422686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner TypeStr[0] == '.', 0); 422786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner} 4228a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4229a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType 4230a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { 4231a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Perform the usual unary conversions. We do this early so that 4232a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // integral promotions to "int" can allow us to exit early, in the 4233a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // lhs == rhs check. Also, for conversion purposes, we ignore any 4234a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // qualifiers. For example, "const float" and "float" are 4235a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // equivalent. 4236a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isPromotableIntegerType()) 4237a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman lhs = getPromotedIntegerType(lhs); 4238a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman else 4239a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman lhs = lhs.getUnqualifiedType(); 4240a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isPromotableIntegerType()) 4241a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhs = getPromotedIntegerType(rhs); 4242a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman else 4243a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhs = rhs.getUnqualifiedType(); 4244a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4245a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // If both types are identical, no conversion is needed. 4246a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs == rhs) 4247a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4248a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4249a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // If either side is a non-arithmetic type (e.g. a pointer), we are done. 4250a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The caller can deal with this (e.g. pointer + int). 4251a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) 4252a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4253a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4254a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // At this point, we have two different arithmetic types. 4255a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4256a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Handle complex types first (C99 6.3.1.8p1). 4257a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isComplexType() || rhs->isComplexType()) { 4258a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // if we have an integer operand, the result is the complex type. 4259a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { 4260a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the rhs to the lhs complex type. 4261a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4262a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4263a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { 4264a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the lhs to the rhs complex type. 4265a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4266a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4267a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // This handles complex/complex, complex/float, or float/complex. 4268a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // When both operands are complex, the shorter operand is converted to the 4269a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // type of the longer, and that is the type of the result. This corresponds 4270a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // to what is done when combining two real floating-point operands. 4271a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The fun begins when size promotion occur across type domains. 4272a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // From H&S 6.3.4: When one operand is complex and the other is a real 4273a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // floating-point type, the less precise type is converted, within it's 4274a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // real or complex domain, to the precision of the other type. For example, 4275a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // when combining a "long double" with a "double _Complex", the 4276a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // "double _Complex" is promoted to "long double _Complex". 4277a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman int result = getFloatingTypeOrder(lhs, rhs); 4278a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4279a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (result > 0) { // The left side is bigger, convert rhs. 4280a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); 4281a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (result < 0) { // The right side is bigger, convert lhs. 4282a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); 4283a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4284a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // At this point, lhs and rhs have the same rank/size. Now, make sure the 4285a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // domains match. This is a requirement for our implementation, C99 4286a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // does not require this promotion. 4287a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs != rhs) { // Domains don't match, we have complex/float mix. 4288a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isRealFloatingType()) { // handle "double, _Complex double". 4289a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4290a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else { // handle "_Complex double, double". 4291a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4292a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4293a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4294a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; // The domain/size match exactly. 4295a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4296a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Now handle "real" floating types (i.e. float, double, long double). 4297a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { 4298a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // if we have an integer operand, the result is the real floating type. 4299a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isIntegerType()) { 4300a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert rhs to the lhs floating point type. 4301a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4302a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4303a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (rhs->isComplexIntegerType()) { 4304a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert rhs to the complex floating point type. 4305a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return getComplexType(lhs); 4306a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4307a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isIntegerType()) { 4308a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert lhs to the rhs floating point type. 4309a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4310a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4311a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isComplexIntegerType()) { 4312a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert lhs to the complex floating point type. 4313a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return getComplexType(rhs); 4314a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4315a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // We have two real floating types, float/complex combos were handled above. 4316a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Convert the smaller operand to the bigger result. 4317a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman int result = getFloatingTypeOrder(lhs, rhs); 4318a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (result > 0) // convert the rhs 4319a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4320a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman assert(result < 0 && "illegal float comparison"); 4321a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; // convert the lhs 4322a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4323a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { 4324a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Handle GCC complex int extension. 4325a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); 4326a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); 4327a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman 4328a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhsComplexInt && rhsComplexInt) { 4329a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (getIntegerTypeOrder(lhsComplexInt->getElementType(), 4330a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhsComplexInt->getElementType()) >= 0) 4331a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; // convert the rhs 4332a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4333a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (lhsComplexInt && rhs->isIntegerType()) { 4334a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the rhs to the lhs complex type. 4335a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return lhs; 4336a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (rhsComplexInt && lhs->isIntegerType()) { 4337a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // convert the lhs to the rhs complex type. 4338a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return rhs; 4339a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4340a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4341a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Finally, we have two differing integer types. 4342a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The rules for this case are in C99 6.3.1.8 4343a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman int compare = getIntegerTypeOrder(lhs, rhs); 4344a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman bool lhsSigned = lhs->isSignedIntegerType(), 4345a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman rhsSigned = rhs->isSignedIntegerType(); 4346a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman QualType destType; 4347a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman if (lhsSigned == rhsSigned) { 4348a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // Same signedness; use the higher-ranked type 4349a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = compare >= 0 ? lhs : rhs; 4350a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (compare != (lhsSigned ? 1 : -1)) { 4351a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The unsigned type has greater than or equal rank to the 4352a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // signed type, so use the unsigned type 4353a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = lhsSigned ? rhs : lhs; 4354a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else if (getIntWidth(lhs) != getIntWidth(rhs)) { 4355a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The two types are different widths; if we are here, that 4356a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // means the signed type is larger than the unsigned type, so 4357a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // use the signed type. 4358a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = lhsSigned ? lhs : rhs; 4359a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } else { 4360a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // The signed type is higher-ranked than the unsigned type, 4361a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // but isn't actually any bigger (like unsigned int and long 4362a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // on most 32-bit systems). Use the unsigned type corresponding 4363a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman // to the signed type. 4364a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); 4365a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman } 4366a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman return destType; 4367a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman} 4368