ASTContext.cpp revision dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9c
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"
15bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck#include "clang/AST/CharUnits.h"
1649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
17980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
18aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
21ea1471e0e967548c596a71469702f8846dbaf3c0John McCall#include "clang/AST/ExprCXX.h"
222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2319cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
241b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
25a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
27f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
2885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
296fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
30f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
3129445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3861710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
39444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel Dunbar                       const TargetInfo &t,
40e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
411b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       bool FreeMem, unsigned size_reserve) :
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
442bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  NSConstantStringTypeDecl(0),
45782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
46083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
47bf1a028246d884a540aeafa38e89be59a269b072John McCall  NullTypeSourceInfo(QualType()),
48c6fbbedb3e90ff2f04828c36fd839e01468679f5Douglas Gregor  SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
50ac9590effa90406767a544005ed1de52e258306bTed Kremenek  BuiltinInfo(builtins),
51ac9590effa90406767a544005ed1de52e258306bTed Kremenek  DeclarationNames(*this),
52ac9590effa90406767a544005ed1de52e258306bTed Kremenek  ExternalSource(0), PrintingPolicy(LOpts),
530c01d18094100db92d38daa923c95661512db203John McCall  LastSDM(0, 0) {
540f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCIdRedefinitionType = QualType();
550f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCClassRedefinitionType = QualType();
56369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian  ObjCSelRedefinitionType = QualType();
571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
58e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
5914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
60e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
61e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
633478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
643478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
653478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
670054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  if (!FreeMemory) {
680054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor    // Call all of the deallocation functions.
690054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor    for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
700054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor      Deallocations[I].first(Deallocations[I].second);
710054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  }
720054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
737d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  // Release all of the memory associated with overridden C++ methods.
747d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
757d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor         OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
767d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor       OM != OMEnd; ++OM)
777d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    OM->second.Destroy();
783478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
79bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek  if (FreeMemory) {
80bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    // Deallocate all the types.
81bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    while (!Types.empty()) {
82bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      Types.back()->Destroy(*this);
83bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      Types.pop_back();
84bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    }
85b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
86bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    for (llvm::FoldingSet<ExtQuals>::iterator
87bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek         I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
88bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      // Increment in loop to prevent using deallocated memory.
890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Deallocate(&*I++);
90bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    }
910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
92503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    for (llvm::DenseMap<const ObjCContainerDecl*,
93503524acc73f2f8280080ca0d200377406001b65Ted Kremenek         const ASTRecordLayout*>::iterator
94503524acc73f2f8280080ca0d200377406001b65Ted Kremenek         I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
95503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      // Increment in loop to prevent using deallocated memory.
96503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
97503524acc73f2f8280080ca0d200377406001b65Ted Kremenek        R->Destroy(*this);
98503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    }
99b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
100b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
101dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
102dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // even when using the BumpPtrAllocator because they can contain
103dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // DenseMaps.
104dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
105dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
106dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
107dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
108dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
109dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
110dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek
111ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
1121ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
1131ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         NNSEnd = NestedNameSpecifiers.end();
115bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek       NNS != NNSEnd; ) {
116bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    // Increment in loop to prevent using deallocated memory.
1171ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
118bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek  }
119ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
120ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
121ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
122ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
123b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1260054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
1270054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
1280054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
1290054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
1322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
1332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
1387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
139dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
1401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
141dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
142dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
143dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
144dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
145c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
148dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
151dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
152dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
153dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
154dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
155dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
156dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
157dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
158dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
159dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
161dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
170e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
1716b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
172e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
1736b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
18515b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
2021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
20764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
2082df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
2092df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
2102df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
2112df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
2123a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
2133a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
2143a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
2153a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
21664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
217f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
218f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
219f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
220f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
221f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
222f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
223f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
224f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
225f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
226f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2278e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
228898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
229898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
230898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
231898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
232898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
233898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
234898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
235898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
2368e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Placeholder type for C++0x auto declarations whose real type has
238e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // not yet been deduced.
239e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
2458e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2467e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
248de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
249de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
250de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
25113dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = QualType();
2521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25313dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
254de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
255de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
25613dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
25714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
258a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
26133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2626e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2636e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2646e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
267251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
268663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
2697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
270663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
2717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
2727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
2737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
2741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2757caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
2767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
279251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
280251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor                                                TemplateSpecializationKind TSK) {
2817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
2827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
2837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
2847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
285251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
286251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    = new (*this) MemberSpecializationInfo(Tmpl, TSK);
2877caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2887caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2897ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
290ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
2917ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
292ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
293ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
2940d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
2951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2960d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
2970d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
2980d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
2990d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
300ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
301ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
302ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
303ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
304ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
305ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
306ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
307ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
308ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
309ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
310ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
311ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
312ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
313ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
314ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
315ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
316ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
317ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
318ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
319ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
320ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
321ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
322ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
323ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
3240d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
3250d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
326d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
327d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
328d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
329d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
330d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
3311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
332d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
333d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
334d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
335d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
336d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
337d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
338d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
339d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
340d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
342d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
343d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
344d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
3457d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
3467d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
3477d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
3487d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
3497d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
3507d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
3517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
3537d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3547d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
3567d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
3577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
3587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
3597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
3607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
3617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
3637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3647d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
3667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
3677d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
3687d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3697d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace {
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  class BeforeInTranslationUnit
3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    : std::binary_function<SourceRange, SourceRange, bool> {
3732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    SourceManager *SourceMgr;
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  public:
3762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
3771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    bool operator()(SourceRange X, SourceRange Y) {
3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
3802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
3812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  };
3822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
3832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
384464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
385464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
386464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
387a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
388b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
389b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
390b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
391183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
392b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
393b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
394b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
395b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
396b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
397b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
398b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
399b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
400b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
4018b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
402af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
403af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
4045d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
4055d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
4068b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
407dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
408dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
40940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
410bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    Align = std::max(Align, AA->getMaxAlignment());
411dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
412af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
413af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
4146217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
4155d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
4165d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
4175d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
4185d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
4195d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
4205d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
4216deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      unsigned MinWidth = Target.getLargeArrayMinWidth();
4226deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      unsigned ArrayAlign = Target.getLargeArrayAlign();
4236deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      if (isa<VariableArrayType>(T) && MinWidth != 0)
4246deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola        Align = std::max(Align, ArrayAlign);
4256deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
4266deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola        unsigned Size = getTypeSize(CT);
4276deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola        if (MinWidth != 0 && MinWidth <= Size)
4286deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola          Align = std::max(Align, ArrayAlign);
4296deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
4304cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
431dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
432dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
433dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
434dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
435dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
43605f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
43705f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      // In the case of a field in a packed struct, we want the minimum
43805f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      // of the alignment of the field and the alignment of the struct.
43905f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      Align = std::min(Align,
44005f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis        getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
44105f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
442af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
443dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
4448b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  return CharUnits::fromQuantity(Align / Target.getCharWidth());
445af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
446b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
447ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
448ea1471e0e967548c596a71469702f8846dbaf3c0John McCallASTContext::getTypeInfoInChars(const Type *T) {
449ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
450ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
451ea1471e0e967548c596a71469702f8846dbaf3c0John McCall                        CharUnits::fromQuantity(Info.second / getCharWidth()));
452ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
453ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
454ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
455ea1471e0e967548c596a71469702f8846dbaf3c0John McCallASTContext::getTypeInfoInChars(QualType T) {
456ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
457ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
458ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
459a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
460a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
4610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
4620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
4630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
4640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
465d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
4661d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
4675e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
4685e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
469a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
47072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
47172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
47218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
47372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
47472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
47518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
47672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
47772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
4785d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
4795d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
48018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
48118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
48218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
48318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
48418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
48572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
486fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
48718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
48818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
48918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
49018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
491fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
4921d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49498be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
4959e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
496030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
497030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
4985c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
499213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
500030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
5019fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
5029fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
5039fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
5044bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
5056fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
5066fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
5078eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
5089fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
5099fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
5109fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
511030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
512030d8846c7e520330007087e949f621989876e3aChris Lattner  }
5135d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
5149e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
515a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
516692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
517d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
51818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
51918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
52018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
52118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
52218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
5249e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
5259e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
5266f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
527692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
528692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
529692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
5306f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
5319e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
5329e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
5336f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
53464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
53564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
53664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
53764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
538f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
539f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
540f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
541f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
542f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
543f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
544f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
545f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
546692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
5476f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
5489e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
5499e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
5506f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
551692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
5526f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
5539e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
5549e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
5556f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
556692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
5576f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
5589e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
5599e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
5606f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
561692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
5626f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
5639e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
5649e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
5656f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
566ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
567ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
568ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
569ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
570ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
5716f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
5729e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
5739e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
5746f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5756f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
5765426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
5775426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
5786f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5796f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
5809e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
5819e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
5826f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5836e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
5846e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
5856e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
5861590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
587a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
588bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
589d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
5905426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
591f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
5926f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
593485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
594485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
595485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
596485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
597485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
598485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
5995d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
6005d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
6015d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
6025d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
6035d484e8cf710207010720589d89602233de61d01Sebastian Redl    unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
6045d484e8cf710207010720589d89602233de61d01Sebastian Redl    Width = Target.getPointerWidth(AS);
6055d484e8cf710207010720589d89602233de61d01Sebastian Redl    Align = Target.getPointerAlign(AS);
6065d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
6075d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
608f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
609f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
6105426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
611f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
612f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
613f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
614f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
615f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
6161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
6171cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
6181cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
619f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
620f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
6211cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
6221cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
623f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
6245d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
6255d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
6265d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
62898be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
6299e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
6305d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
6315d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
6325d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
633c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
634c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
63544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
6361d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
63744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
63844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
63944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
64044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
64144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
64272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
64372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
6441d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
6451d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
6461d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
6478389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
6488389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
6498389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
6508389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6521d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
6537176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
6547176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
6551d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
6567176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
6577176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
6587176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
659dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
660a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
6617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
6629fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
66349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
66449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
66549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
66618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
66718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
66840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
669bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      Align = std::max(Aligned->getMaxAlignment(),
670bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt                       getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
67118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
67218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
67318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
6747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
6757176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
67618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
67718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
67818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
67918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
68018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
68118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
68218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
68318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
684395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
685395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
686395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
687395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
688465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
689465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
69318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
69418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
69518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
69618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
69718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
69818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
700464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
7019e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
702a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
703a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
704bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
705bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
706bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen DyckCharUnits ASTContext::getTypeSizeInChars(QualType T) {
707199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
708bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
709bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen DyckCharUnits ASTContext::getTypeSizeInChars(const Type *T) {
710199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
711bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
712bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
71316e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
71486fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
71586fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken DyckCharUnits ASTContext::getTypeAlignInChars(QualType T) {
71686fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck  return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
71786fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
71886fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken DyckCharUnits ASTContext::getTypeAlignInChars(const Type *T) {
71986fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck  return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
72086fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
72186fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
72234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
72334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
72434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
72534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
72634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
72734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
7281eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
7291eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
730183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
7311eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
7321eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
7331eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
7341eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
7351eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
73634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
73734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
73834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
739a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
740a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
741a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
742a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
743a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
744f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
745a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
746a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
747a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
748a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
749a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
750a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
751a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
752a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
753a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
754a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
755a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
756a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
7578e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
7588e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
7598e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
7608e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
76111062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
7628e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
7638e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian         E = OI->ivar_end(); I != E; ++I) {
7648e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian     Ivars.push_back(*I);
7658e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
76611062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian
76711062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  CollectNonClassIvars(OI, Ivars);
7688e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
7698e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
77011062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian/// CollectNonClassIvars -
77111062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian/// This routine collects all other ivars which are not declared in the class.
7727d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek/// This includes synthesized ivars (via @synthesize) and those in
7737d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek//  class's @implementation.
7749820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
77511062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanianvoid ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
7769820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
7770e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  // Find ivars declared in class extension.
7780e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
7790e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian    for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
7800e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian         E = CDecl->ivar_end(); I != E; ++I) {
7810e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian      Ivars.push_back(*I);
7820e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian    }
7830e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  }
78411062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian
7857d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek  // Also add any ivar defined in this class's implementation.  This
7867d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek  // includes synthesized ivars.
78711062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
78811062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian    for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
78911062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian         E = ImplDecl->ivar_end(); I != E; ++I)
79011062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian      Ivars.push_back(*I);
79111062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  }
7929820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
7939820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
794e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
795e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
796e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
797432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
798e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
799e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
800e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OI->protocol_end(); P != PE; ++P) {
801e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
802432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
803e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
804b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
805b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian        Protocols.insert(*P);
806e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
807e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
808b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
809e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
810e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
811e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
812e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
813e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
814e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
815e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
816e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
817e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
818e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
819b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
820e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
821e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
822e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
823432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
824e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
825e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
826e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
827e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
828b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
829e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
830e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
831e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
832432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
833e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
834e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
835e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
836e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
837e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
838e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
839e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
8403bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanianunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
8413bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
8423bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
843b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  if (const ObjCCategoryDecl *CDecl = OI->getClassExtension())
844b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
845b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
8463bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
8473bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
8483bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
849b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
850b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
8518e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8528e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8538e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
8558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
8568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
8608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
8638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
8648a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8658a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8668a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
8688a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8698a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8708a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
8718a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
8728a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
8738a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
8748a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
8758a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
8768a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8778a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
8788a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
8798a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
8808a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
8818a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
8828a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8838a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
884a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Allocate an uninitialized TypeSourceInfo.
885b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
886a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// The caller should initialize the memory held by TypeSourceInfo using
887b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
888b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
889b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
890b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
891b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
892a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
893109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall                                                 unsigned DataSize) {
894109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
895109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
896109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
897109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
898a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
899109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
900a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
901a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
902a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
903a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
904b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
905b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
906a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
907a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall                                                     SourceLocation L) {
908a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
909a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  DI->getTypeLoc().initialize(L);
910a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
911a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
912a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
913b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
914b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
915b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
916b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
917b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
918b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
919b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
920b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
921b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
922b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
923a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
924a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
925a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
926a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
9270953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
9280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  unsigned Fast = Quals.getFastQualifiers();
9290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeFastQualifiers();
9300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
9320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
9330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQuals::Profile(ID, TypeNode, Quals);
9340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  void *InsertPos = 0;
9350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
9360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert(EQ->getQualifiers() == Quals);
9370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType T = QualType(EQ, Fast);
9380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return T;
9390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
9400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9416b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
9420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQualNodes.InsertNode(New, InsertPos);
9430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType T = QualType(New, Fast);
9440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return T;
9450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
9460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9470953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getVolatileType(QualType T) {
9480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanT = getCanonicalType(T);
9490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (CanT.isVolatileQualified()) return T;
9500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
9520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
9530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addVolatile();
9540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
9560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
9570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
958f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
959f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
960f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
961f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
962b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
9630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
9640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
9650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
9660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
9690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
9700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
9710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
9720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
975ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
976ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
977b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
9780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                       Qualifiers::GC GCAttr) {
979d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
980b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
981d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
9821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9834027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  if (T->isPointerType()) {
9846217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
98558f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
9864027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
9874027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
9884027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
9894027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
9920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
9930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
9940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
9970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
9980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
9990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
10000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1003d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1004a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1005264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindolastatic QualType getExtFunctionType(ASTContext& Context, QualType T,
1006264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                        const FunctionType::ExtInfo &Info) {
10070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType ResultType;
100843c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  if (const PointerType *Pointer = T->getAs<PointerType>()) {
100943c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    QualType Pointee = Pointer->getPointeeType();
1010264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    ResultType = getExtFunctionType(Context, Pointee, Info);
101143c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (ResultType == Pointee)
101243c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1013ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1014ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    ResultType = Context.getPointerType(ResultType);
101543c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  } else if (const BlockPointerType *BlockPointer
101643c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor                                              = T->getAs<BlockPointerType>()) {
101743c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    QualType Pointee = BlockPointer->getPointeeType();
1018264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    ResultType = getExtFunctionType(Context, Pointee, Info);
101943c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (ResultType == Pointee)
102043c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1021ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1022ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    ResultType = Context.getBlockPointerType(ResultType);
1023ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor   } else if (const FunctionType *F = T->getAs<FunctionType>()) {
1024264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    if (F->getExtInfo() == Info)
102543c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1026ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
102743c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
1028ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor      ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
1029264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                  Info);
10300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    } else {
103143c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
10320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      ResultType
1033ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor        = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1034ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getNumArgs(), FPT->isVariadic(),
1035ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getTypeQuals(),
1036ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->hasExceptionSpec(),
1037ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->hasAnyExceptionSpec(),
1038ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getNumExceptions(),
1039ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->exception_begin(),
1040264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                  Info);
10410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
104243c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  } else
104343c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    return T;
1044ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1045ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1046ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor}
1047ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1048ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas GregorQualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
1049425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1050264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getExtFunctionType(*this, T,
1051264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                 Info.withNoReturn(AddNoReturn));
1052ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor}
1053ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1054ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas GregorQualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
1055425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1056264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getExtFunctionType(*this, T,
1057264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                            Info.withCallingConv(CallConv));
10582455636163fdd18581d7fdae816433f886d88213Mike Stump}
10592455636163fdd18581d7fdae816433f886d88213Mike Stump
1060425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael EspindolaQualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1061425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1062425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  return getExtFunctionType(*this, T,
1063425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                                 Info.withRegParm(RegParm));
1064425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola}
1065425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
10665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
10675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
10685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
10771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1081467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1082f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
10831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1086f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10886b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
10905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
11051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1109467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1110f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1114f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11166b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
11235618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
11245618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
1125296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1126296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
11275618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
11285618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
11295618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11315618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
11325618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
11335618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11345618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
11351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
11375618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
11385618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1139467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
11405618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11425618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
11435618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
11445618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1145f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11465618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
11476b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
11486b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
11495618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
11505618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
11515618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
11525618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
11535618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
11557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
115654e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
116054e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
11647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
11667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
116754e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
116854e14c4db764c0636160d26c5bbf491637c83a76John McCall
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
117254e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
117354e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
117454e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
11757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
11767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
11777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
11787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
11797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
11817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
11826b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
118354e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
118454e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
11857c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
11867c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
118754e14c4db764c0636160d26c5bbf491637c83a76John McCall
11887c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
11897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
11907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
11917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
11927c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
11937c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
11947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
11957c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
11967c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
119754e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
11987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
11997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
12007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
12017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12027c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
12037c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
120454e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
120554e14c4db764c0636160d26c5bbf491637c83a76John McCall
12067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
12077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
12087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
120954e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
121054e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
121154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
12127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
12157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1216f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12196b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
12206b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1226f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1227f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
1229f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1230f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1231f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1232f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1233f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1234f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1235f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1236f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1237f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1238f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1239f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1240f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1241f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
124287c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1243f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1244f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1245f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1246f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1247f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1248f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1249f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
12506b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
12516b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1252f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1253f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1254f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1255f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1256f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1258fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
12591eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
126038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1261c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1262c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1263923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1264923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1265587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1266587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
126738aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
126838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
126938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
127038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
12730be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
12741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
12761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
12777192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
12791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1283467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!EltTy.isCanonical()) {
12841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1285c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
12887192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1289f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12926b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
12936b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
12947192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1299bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1300bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
13017e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
13027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1303c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
13047e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          unsigned EltTypeQuals,
13057e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          SourceRange Brackets) {
1306c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1307c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1308715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  QualType CanonType;
1309715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
1310715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  if (!EltTy.isCanonical()) {
1311715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor    if (NumElts)
1312715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor      NumElts->Retain();
1313715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor    CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1314715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor                                     EltTypeQuals, Brackets);
1315715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
1316715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
13176b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
1318715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor    VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
1319c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1320c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1321c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1322c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1323c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1324c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1325898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1326898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
132704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
13287e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy,
13297e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                Expr *NumElts,
1330898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
13317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                unsigned EltTypeQuals,
13327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                SourceRange Brackets) {
1333cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  assert((!NumElts || NumElts->isTypeDependent() ||
1334cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor          NumElts->isValueDependent()) &&
1335898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1336898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
133704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  void *InsertPos = 0;
1338cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  DependentSizedArrayType *Canon = 0;
1339789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
1340cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1341cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  if (NumElts) {
1342cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // Dependently-sized array types that do not have a specified
1343cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // number of elements will have their sizes deduced from an
1344cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // initializer.
1345cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1346cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor                                     EltTypeQuals, NumElts);
1347cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1348cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1349cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
1350cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
135104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *New;
135204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  if (Canon) {
135304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // We already have a canonical version of this array type; use it as
135404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // the canonical type for a newly-built type.
13556b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
13566b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
13576b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                              NumElts, ASM, EltTypeQuals, Brackets);
135804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  } else {
135904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    QualType CanonEltTy = getCanonicalType(EltTy);
136004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    if (CanonEltTy == EltTy) {
13616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
13626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedArrayType(*this, EltTy, QualType(),
13636b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                NumElts, ASM, EltTypeQuals, Brackets);
1364cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1365789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      if (NumElts) {
1366789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        DependentSizedArrayType *CanonCheck
1367789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor          = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1368789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        assert(!CanonCheck && "Dependent-sized canonical array type broken");
1369789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        (void)CanonCheck;
1370cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor        DependentSizedArrayTypes.InsertNode(New, InsertPos);
1371789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      }
137204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    } else {
137304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
137404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  ASM, EltTypeQuals,
137504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  SourceRange());
13766b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
13776b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedArrayType(*this, EltTy, Canon,
13786b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                NumElts, ASM, EltTypeQuals, Brackets);
137904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    }
138004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  }
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1382898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1383898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1384898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1385898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1386c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1387c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1388c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1389c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
13900be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1391c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1392c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (IncompleteArrayType *ATP =
1394c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1395c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1396c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1397c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1398c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1399c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1400c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1401467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!EltTy.isCanonical()) {
1402f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
14032bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1404c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1405c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1406c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1407c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1408f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14092bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1410c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
14116b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  IncompleteArrayType *New = new (*this, TypeAlignment)
14126b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
1413c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1414c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1415c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1416c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1417fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1418fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
141973322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
142073322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
142182287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
142282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson                                   bool IsAltiVec, bool IsPixel) {
14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1425f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
142673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
14271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
143082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  VectorType::Profile(ID, vecType, NumElts, Type::Vector,
143182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    IsAltiVec, IsPixel);
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
143982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
144082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Canonical = getVectorType(getCanonicalType(vecType),
144182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson      NumElts, false, false);
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1445f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14476b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
144882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1454213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
145573322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1456213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
145773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1459f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1460213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
146273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
146373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
146482287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
146573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
146673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
146773322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
146873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
146973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
147073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
147173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
1472467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
1473213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
14741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
147573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
147673322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1477f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
147873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
14796b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
14806b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
148173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
148273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
148373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
148473322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
148573322924127c873c13101b705dd823f5539ffa5fSteve Naroff
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
14879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    Expr *SizeExpr,
14889cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    SourceLocation AttrLoc) {
14892ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
14912ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14932ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
14942ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
14952ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
14962ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
14972ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
14982ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
14992ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
15006b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
15016b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
15026b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
15032ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
15042ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
15052ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
15066b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
15076b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
15086b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
1509789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1510789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
1511789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1512789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1513789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
15142ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
15152ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
15162ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
15172ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
15186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
15196b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
15202ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
15212ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
15249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
15259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
152772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1529264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael EspindolaQualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1530264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                            const FunctionType::ExtInfo &Info) {
1531264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  const CallingConv CallConv = Info.getCC();
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1535264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
15361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
153972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
15405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
15411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1543ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
154404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
1545264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
1546264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
1547264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
155072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
155172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1552f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15556b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
1556264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    FunctionNoProtoType(ResultTy, Canonical, Info);
15575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
155872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
15595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
15635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
156461710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1565971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1566465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     unsigned TypeQuals, bool hasExceptionSpec,
1567465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     bool hasAnyExceptionSpec, unsigned NumExs,
1568264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                     const QualType *ExArray,
1569264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                     const FunctionType::ExtInfo &Info) {
1570264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  const CallingConv CallConv= Info.getCC();
15715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
157472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1575465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                             TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1576264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                             NumExs, ExArray, Info);
15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
158072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1582465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1583465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
158454e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
158654e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
1590465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
159204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
159654e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
1597465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1598f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1599beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
160047259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                isVariadic, TypeQuals, false,
1601264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                false, 0, 0,
1602264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
1603465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
16045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
160572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
160672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1607f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1609465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
161072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1611465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
1612465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // end of them.
16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionProtoType *FTP =
1614465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1615465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumArgs*sizeof(QualType) +
16166b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                 NumExs*sizeof(QualType), TypeAlignment);
161772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1618465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                              TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1619264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                              ExArray, NumExs, Canonical, Info);
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
162172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
16263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
16273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
16283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
16293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
16303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
16313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
16323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
16333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
16343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
16353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
16363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
16373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
16383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
16393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
16403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
16413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              QualType TST) {
16423cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
16433cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
16443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
16453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else if (CXXRecordDecl *PrevDecl
16463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall               = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
16473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
16483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
16493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
16503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
165131f17ecbef57b5679c017c375db330546b7b5145John McCall    Decl->TypeForDecl =
165231f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
16533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Types.push_back(Decl->TypeForDecl);
16543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
16553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
16563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
16573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
16582ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
16592ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
1660becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCallQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
16611e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
1662becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
16631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
166419c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
16652ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1666becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
1667becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
1668becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
1669becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
167019c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1671becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Record->getPreviousDeclaration() &&
1672becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
1673becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
1674becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
167519c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1676becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Enum->getPreviousDeclaration() &&
1677becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
1678becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
167919c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
1680ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1681ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
16829fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
1683becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
168449aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1685becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  Types.push_back(Decl->TypeForDecl);
168649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
16872ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
16882ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
16895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
16905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
169119c8576b7328f4dc2d07682f5da552875c1912efJohn McCallQualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
16931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1694f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
16956b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Decl->TypeForDecl = new(*this, TypeAlignment)
16966b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
16975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
16985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
170149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
170249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
170349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
170449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                                         QualType Replacement) {
1705467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
170649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
170749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
170849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
170949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
171049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
171149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
171249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
171349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
171449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
171549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
171649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
171749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
171849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
171949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
172049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
172149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
172249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
172349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
1724fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
17251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
172676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
17271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
172876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
1729fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1730fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
173176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
1732fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
17331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
1734fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1735fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1736fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1737fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
17381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  if (Name) {
174076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
17416b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
17426b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
1743789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1744789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
1745789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1746789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
1747789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
174876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
17496b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
17506b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
1751fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1752fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1753fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1754fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1755fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1756fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1757fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
17583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
17593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
17603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
17613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
17623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              QualType CanonType) {
17633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
17643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
17653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
17663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
17673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
17683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
17693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
17703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
17713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
17723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
17733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
17743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
17753cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
17761eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
17777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
1778d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
177931f17ecbef57b5679c017c375db330546b7b5145John McCall                                          QualType Canon,
178031f17ecbef57b5679c017c375db330546b7b5145John McCall                                          bool IsCurrentInstantiation) {
1781d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
1782d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1783833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  llvm::SmallVector<TemplateArgument, 4> ArgVec;
1784833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
1785833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
1786833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
1787833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
178831f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
178931f17ecbef57b5679c017c375db330546b7b5145John McCall                                       Canon, IsCurrentInstantiation);
1790833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
1791833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1792833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
1793833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
17947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
17957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
179631f17ecbef57b5679c017c375db330546b7b5145John McCall                                          QualType Canon,
179731f17ecbef57b5679c017c375db330546b7b5145John McCall                                          bool IsCurrentInstantiation) {
1798b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  if (!Canon.isNull())
1799b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    Canon = getCanonicalType(Canon);
1800b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  else {
180131f17ecbef57b5679c017c375db330546b7b5145John McCall    assert(!IsCurrentInstantiation &&
180231f17ecbef57b5679c017c375db330546b7b5145John McCall           "current-instantiation specializations should always "
180331f17ecbef57b5679c017c375db330546b7b5145John McCall           "have a canonical type");
180431f17ecbef57b5679c017c375db330546b7b5145John McCall
1805b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    // Build the canonical template specialization type.
18061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateName CanonTemplate = getCanonicalTemplateName(Template);
18071275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::SmallVector<TemplateArgument, 4> CanonArgs;
18081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    CanonArgs.reserve(NumArgs);
18091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
18101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
18111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
18121275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // Determine whether this canonical template specialization type already
18131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // exists.
18141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::FoldingSetNodeID ID;
181531f17ecbef57b5679c017c375db330546b7b5145John McCall    TemplateSpecializationType::Profile(ID, CanonTemplate, false,
1816828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                        CanonArgs.data(), NumArgs, *this);
18171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
18181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    void *InsertPos = 0;
18191275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateSpecializationType *Spec
18201275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
18211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18221275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    if (!Spec) {
18231275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // Allocate a new canonical template specialization type.
18241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      void *Mem = Allocate((sizeof(TemplateSpecializationType) +
18251275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                            sizeof(TemplateArgument) * NumArgs),
18266b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                           TypeAlignment);
182731f17ecbef57b5679c017c375db330546b7b5145John McCall      Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
18281275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                                  CanonArgs.data(), NumArgs,
1829b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                                                  Canon);
18301275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Types.push_back(Spec);
18311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
18321275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
18331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1834b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    if (Canon.isNull())
1835b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor      Canon = QualType(Spec, 0);
18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(Canon->isDependentType() &&
18371275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           "Non-dependent template-id type must have a canonical type");
1838b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  }
1839fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
18401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
18411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
18421275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
18431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
184440808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
18456b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
18461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
184731f17ecbef57b5679c017c375db330546b7b5145John McCall    = new (Mem) TemplateSpecializationType(*this, Template,
184831f17ecbef57b5679c017c375db330546b7b5145John McCall                                           IsCurrentInstantiation,
184931f17ecbef57b5679c017c375db330546b7b5145John McCall                                           Args, NumArgs,
1850828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                           Canon);
18511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
185255f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
18531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
185455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
185555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
18561eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1857465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
1858465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
1859465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              QualType NamedType) {
1860e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1861465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
1862e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1863e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
1864465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1865e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1866e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1867e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1868789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
1869789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
1870789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
1871465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1872465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
1873789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
1874789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
1875789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1876465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
1877e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1878465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
1879e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1880e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1881e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
18824a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
18834a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
18844a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
18854a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          QualType Canon) {
1886d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1887d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1888d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1889d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
18904a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
18914a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
18924a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
18934a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
18944a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
18954a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
1896d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1897d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1898d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
18994a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
1900d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1901d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
19024714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
19034714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1904d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1905d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1906d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
19074a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
1908d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
19094714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
19101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
1911d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1912d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
19131eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
19144a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
19154a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
19164a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 const TemplateSpecializationType *TemplateId,
19174a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 QualType Canon) {
19181734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
19191734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
1920789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
19214a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
1922789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1923789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
19244714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
19254714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1926789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
1927789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
1928789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
19291734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
19301734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
19311734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
19324a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
19334a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
19344a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
19354a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword ||
19364a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor        CanonType != QualType(TemplateId, 0)) {
19371734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
1938183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = CanonType->getAs<TemplateSpecializationType>();
19391734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
19401734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
19414a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
19421734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
19431734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
19444714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    DependentNameType *CheckT
19454714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor      = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1946789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
1947789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
19481734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
19494a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
19501734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
19514714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
19521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
19531734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
19541734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
195588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
195688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
195788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
195888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
19592e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
196088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
196188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1962c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
196354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
196454e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
196554e14c4db764c0636160d26c5bbf491637c83a76John McCall
196654e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
196754e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
196854e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
196954e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
197054e14c4db764c0636160d26c5bbf491637c83a76John McCall}
197154e14c4db764c0636160d26c5bbf491637c83a76John McCall
197254e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
197388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
197488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
19751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
197688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
197788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
197888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
197988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
198088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
198188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
198288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
198388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1984c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
1985c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
1986c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       unsigned NumProtocols) {
1987c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
1988c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
1989c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
1990c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
1991d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1992c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
1993c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
1994c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
1995d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
1996c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
1997c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
1998d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1999c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
2000c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
200154e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2002c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2003c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
2004c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
20050237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
20060237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
200754e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
200854e14c4db764c0636160d26c5bbf491637c83a76John McCall
200954e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2010c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2011c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
201254e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
2013c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2014c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
201554e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
201654e14c4db764c0636160d26c5bbf491637c83a76John McCall
201754e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
2018c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
201954e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
202054e14c4db764c0636160d26c5bbf491637c83a76John McCall
2021c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
2022c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2023fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2024c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
2025c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2027c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2028c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
2029c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2030d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
203188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2032c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2033c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
2034c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
20354b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2036c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
20371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20384b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2039c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
2040c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
20414b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
20421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2043c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
204454e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2045c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
2046c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
204754e14c4db764c0636160d26c5bbf491637c83a76John McCall
2048c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
2049c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
205054e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
205154e14c4db764c0636160d26c5bbf491637c83a76John McCall
2052c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
2053c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2054c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
2055c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
205624fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
205724fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
2058c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
205924fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
206024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
206124fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
2062c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCInterfaceType - Return the unique reference to the type for the
2063c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// specified ObjC interface decl. The list of protocols is optional.
2064c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2065c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (Decl->TypeForDecl)
2066c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(Decl->TypeForDecl, 0);
2067c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
2068c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // FIXME: redeclarations?
2069c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2070c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2071c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Decl->TypeForDecl = T;
2072c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2073c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2074c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
2075c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
207672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
207772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
20789752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
20791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
20809752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
208172564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
2082dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2083b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2084b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2085b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
20861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2087b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2088b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2089b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2090b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2091b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2092b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
20936b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
2094b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
2095b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2096b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
2097b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
20986b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
20996b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
2100b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2101b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2102b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2103b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2104dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
21056b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
2106dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
21079752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
21089752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2109d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2110d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
21119752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
21129752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
21139752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
21141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
21159752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
2116d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
2117f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
21186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
21199752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
21209752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2121d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2122d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
212360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
212460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
212560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
2126a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2127a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
21281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
212960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
213060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
213160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
213260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
213360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
213460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
213560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
213660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
213760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
213860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
213960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
214060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
214160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
214260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
214360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
21441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
214560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
21461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
214860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
214960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (e->isLvalue(Context) == Expr::LV_Valid)
215060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
21511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
215360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
215460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2155395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2156395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2157395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2159395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
2160395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) {
2161dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
21629d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  if (e->isTypeDependent()) {
21639d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
21649d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
21651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21669d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
21679d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
21689d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
21699d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
21709d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
21719d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
21726b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
21739d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
21749d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
21759d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
21769d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
21776b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
21789d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
21799d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
21809d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
21819d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2182dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
21836b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
2184dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2185395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2186395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2187395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2188395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
21895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
21905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
2191e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) {
2192d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2193e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2194e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2195e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
21991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
22001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
2201a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
2202b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
22035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
220564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
220664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
220764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
220864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
220964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
221064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
221164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
221264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
221364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
221464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
221564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
221664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
221764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
221864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
22198b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
22208b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
22218b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2222b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
22238b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
22248b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2225e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2226e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2227e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2228e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
222954e14c4db764c0636160d26c5bbf491637c83a76John McCallCanQualType ASTContext::getCanonicalParamType(QualType T) {
223054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
223154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
223254e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
223354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
223454e14c4db764c0636160d26c5bbf491637c83a76John McCall
223554e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
223654e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
223754e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
223854e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
223954e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
224054e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
224154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
224254e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
224354e14c4db764c0636160d26c5bbf491637c83a76John McCall
224454e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
224554e14c4db764c0636160d26c5bbf491637c83a76John McCall}
224654e14c4db764c0636160d26c5bbf491637c83a76John McCall
224777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
224877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
224977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
225077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
225177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
225277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
225350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) {
22540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
22550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ptr = Quals.strip(T);
22560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanType = Ptr->getCanonicalTypeInternal();
22570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
22580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // The canonical internal type will be the canonical type *except*
22590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // that we push type qualifiers down through array types.
22601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If there are no new qualifiers to push down, stop here.
22620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!Quals.hasQualifiers())
226350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType);
2264c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
22650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the type qualifiers are on an array type, get the canonical
22660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type of the array with the qualifiers applied to the element
22670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type.
2268c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
2269c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
22700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
22711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2272c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2273c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
22740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
2275c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
22761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2277c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
227850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
227950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getConstantArrayType(NewEltTy, CAT->getSize(),
228050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getSizeModifier(),
22810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                  CAT->getIndexTypeCVRQualifiers()));
2282c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
228350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
228450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
22850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    IAT->getIndexTypeCVRQualifiers()));
22861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2287898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
228850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
228950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getDependentSizedArrayType(NewEltTy,
2290bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                        DSAT->getSizeExpr() ?
2291bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                          DSAT->getSizeExpr()->Retain() : 0,
229250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getSizeModifier(),
22930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                        DSAT->getIndexTypeCVRQualifiers(),
229487a924e37dc40c6e3e2b33d1dbd646aeccf4c15bDouglas Gregor                        DSAT->getBracketsRange())->getCanonicalTypeInternal());
2295898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2296c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
229750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
2298bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                        VAT->getSizeExpr() ?
2299bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr()->Retain() : 0,
230050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                        VAT->getSizeModifier(),
23010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
230250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                     VAT->getBracketsRange()));
2303c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
2304c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
230528e318cc6008c2bc008f0caee70dc736a03d6289Chandler CarruthQualType ASTContext::getUnqualifiedArrayType(QualType T,
230628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                             Qualifiers &Quals) {
23075535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  Quals = T.getQualifiers();
23089dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const ArrayType *AT = getAsArrayType(T);
23099dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
23105535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth    return T.getUnqualifiedType();
231128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
231228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
231328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  QualType Elt = AT->getElementType();
2314c1ae0a8b0022a9fbb50d3413139f36ec5d8a63beZhongxing Xu  QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
231528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  if (Elt == UnqualElt)
231628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return T;
231728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
23189dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
231928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return getConstantArrayType(UnqualElt, CAT->getSize(),
232028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
232128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
232228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
23239dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
232428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
232528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
232628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
23279dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
23289dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor    return getVariableArrayType(UnqualElt,
23299dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeExpr() ?
23309dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeExpr()->Retain() : 0,
23319dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
23329dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
23339dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
23349dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
23359dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
23369dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
233728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
233828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
233928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
234028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
234128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
234280ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCallDeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
234380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (TemplateDecl *TD = Name.getAsTemplateDecl())
234480ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    return TD->getDeclName();
234580ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
234680ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
234780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
234880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall      return DeclarationNames.getIdentifier(DTN->getIdentifier());
234980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
235080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall      return DeclarationNames.getCXXOperatorName(DTN->getOperator());
235180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
235280ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
235380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
23540bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
23550bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(Storage);
23560bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return (*Storage->begin())->getDeclName();
235780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
235880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
235925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
236025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
236125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
236225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
236397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
236425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
23650bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(!Name.getAsOverloadedTemplate());
23661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
236825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
236925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
237025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
237125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
2372db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2373db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
2374db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
2375db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
2376db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
2377db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
23781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
23791275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
23801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
23811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
23821275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
23831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
23851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
23861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23871275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
2388833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
23891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2390788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
2391788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2392788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
23931275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
2394833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(*Arg.getAsIntegral(),
23951275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
23961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23971275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
2398833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
23991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24001275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
24011275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Allocate in ASTContext
24021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
24031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
24041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
24051275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
24061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
24071275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
24081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument Result;
24101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
24111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Result;
24121275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
24131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
24141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
24151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
24161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
24171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
24181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
24191275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
2420d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
2421d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
24221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
2423d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
2424d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2425d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
2426d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
2427d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
24281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
2429d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2430d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
2431d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2432d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
2433d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
2434d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
2435d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2436d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2437d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
2438d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
2439d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
24401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this, 0,
24411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2442d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
2443d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2444d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2445d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
2446d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
2447d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
2448d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2449d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2450d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
2451d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
2452d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2453d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2454c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2455c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
2456c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
2457a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
2458c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
2459c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2460c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
2461c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
24621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
2464c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
24650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!isa<ArrayType>(CType))
2466c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
24671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
2469c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
2470c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
24711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2472c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
2473c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
247450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
24750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
24760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
24770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ty = Qs.strip(T.getDesugaredType());
24781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2479c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
2480c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
24810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ATy == 0 || Qs.empty())
2482c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
24831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2484c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
2485c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
2486c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2487c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
24880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
24891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2490c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2491c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2492c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
24930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
2494c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2495c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2496c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
24970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
2498898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
24991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
2500898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
2501898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
25021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
2503bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                DSAT->getSizeExpr() ?
2504bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              DSAT->getSizeExpr()->Retain() : 0,
2505898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
25060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
25077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
25081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2509c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
25107e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
2511bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr() ?
25120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getSizeExpr()->Retain() : 0,
2513c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
25140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
25157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
251677c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
251777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
251877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
2519e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
2520e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
2521e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
2522e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
2523e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
2524e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2525e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
2526c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
2527c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
2528c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
2529c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
2530c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2531c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
25321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2533c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
2534e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2535e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
25360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
2537e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
2538e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
25395e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) {
25400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
25410237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer  while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
25420237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer    QT = AT->getElementType();
25430237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer  return Qs.apply(QT);
25445e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
25455e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
2546fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders CarlssonQualType ASTContext::getBaseElementType(const ArrayType *AT) {
2547fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson  QualType ElemTy = AT->getElementType();
25481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2549fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson  if (const ArrayType *AT = getAsArrayType(ElemTy))
2550fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson    return getBaseElementType(AT);
25511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25526183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
25536183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
25546183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
25550de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
25561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
25570de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
25580de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
25590de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
25600de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
25610de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
25620de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
25630de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
25640de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
25650de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
25665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
25675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
2568a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
2569183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
25705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
2571a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2572183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2573183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
2574a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
25755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
25765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
25775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
25785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
25795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
25805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
25821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
2583716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
2584716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
25851361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
25861361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
25871361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
25881361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
25891361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
2590716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
2591f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
2592f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
2593f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
2594f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
2595f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
25961361b11066239ea15764a2a844405352d87296b3Chris Lattner
25971361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
25981361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
25991361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
26001361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
26011361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
26021361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
26035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
26045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
26077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
26087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
26091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
2610a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2611a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
2612a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
26131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2614a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
2615fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
2616a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
2617fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
2618fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
26195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2621f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2622f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
2623f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
2624f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
2625467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
2626f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
2627842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    T = ET->getDecl()->getPromotionType().getTypePtr();
2628f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2629a34267595534a72703290153a6f7e3da1adcec59Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::WChar))
2630a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
2631a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
2632f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
2633f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2634f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2635f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
2636f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2637f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2638f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
26397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
26407cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
2641f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
26427cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
26437cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
26447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
26457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
2646f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
26477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
26487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
2649f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
26507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
26517cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
2652f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
26537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
26547cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
2655f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
26567cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
26577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
2658f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
26592df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
26602df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
26612df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
2662f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
2663f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
2664f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
266504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
266604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
266704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
266804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
266904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
267004e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) {
2671ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
2672ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
2673ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
267404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
267504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
267604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
267704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
267804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
267904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
268004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
268104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
268204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
268304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
268404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
268504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
268604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
268704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
268804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
268904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
269004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
269104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
269204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
269304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
269404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
269504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
269604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
269704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
269804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
2699a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
2700a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2701a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
2702a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2703a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
2704a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
2705842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
2706842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
2707a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
2708a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
2709a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
2710a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
2711a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2712a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2713a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
2714a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
27151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
27167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
27171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
27187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
2719f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
2720f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
27217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
27221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2723f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2724f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
27251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27267cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
27277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
27281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27297cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
27307cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
27317cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
27327cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
27331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27347cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
27357cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
27367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
27377cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
27387cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
27391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27407cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
27417cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
27421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
27437cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
27447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
27457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
27467cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
27477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
27487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
27491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
27517cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
27521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
27537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
27545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
275571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
275679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
275779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders CarlssonCreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
275879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                 SourceLocation L, IdentifierInfo *Id) {
275979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  if (Ctx.getLangOptions().CPlusPlus)
276079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson    return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
276179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
276279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson    return RecordDecl::Create(Ctx, TK, DC, L, Id);
276379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
276479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
27651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
276671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
276771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
27681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
2769465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
277079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
27715cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
277279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
2773f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
27741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
277571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
27760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
2777f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2778f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
277971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
27800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
278171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
27821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
27831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
278444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
278544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
27861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
278744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
2788a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
27891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
27904afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
27912888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
279217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
279344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
279444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
2795838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
279671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
27971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
279871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
27998467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2800b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2801319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
28026217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2803319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2804319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2805319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2806319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
28072bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian// getNSConstantStringType - Return the type used for constant NSStrings.
28082bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz JahanianQualType ASTContext::getNSConstantStringType() {
28092bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  if (!NSConstantStringTypeDecl) {
28102bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl =
2811465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
28122bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                     &Idents.get("__builtin_NSString"));
28132bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->startDefinition();
28142bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28152bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    QualType FieldTypes[3];
28162bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28172bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const int *isa;
28182bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[0] = getPointerType(IntTy.withConst());
28192bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const char *str;
28202bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[1] = getPointerType(CharTy.withConst());
28212bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // unsigned int length;
28222bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[2] = UnsignedIntTy;
28232bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28242bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // Create fields
28252bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    for (unsigned i = 0; i < 3; ++i) {
28262bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
28272bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           SourceLocation(), 0,
28282bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           FieldTypes[i], /*TInfo=*/0,
28292bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*BitWidth=*/0,
28302bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*Mutable=*/false);
28312888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
28322bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      NSConstantStringTypeDecl->addDecl(Field);
28332bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    }
28342bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28352bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->completeDefinition();
28362bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  }
28372bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28382bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  return getTagDeclType(NSConstantStringTypeDecl);
28392bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
28402bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28412bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanianvoid ASTContext::setNSConstantStringType(QualType T) {
28422bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  const RecordType *Rec = T->getAs<RecordType>();
28432bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  assert(Rec && "Invalid NSConstantStringType");
28442bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  NSConstantStringTypeDecl = Rec->getDecl();
28452bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
28462bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28471eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getObjCFastEnumerationStateType() {
2848bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
284944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
2850465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
285179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__objcFastEnumerationState"));
28525cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    ObjCFastEnumerationStateTypeDecl->startDefinition();
28531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2854bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2855bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2856de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
2857bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2858bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2859bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2860bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
28611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
286244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
28631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this,
28641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           ObjCFastEnumerationStateTypeDecl,
28651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           SourceLocation(), 0,
2866a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
28671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
28684afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
28692888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
287017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
287144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
287238c9ab880b843a128f8d895d3e2e24877e178788Fariborz Jahanian    if (getLangOptions().CPlusPlus)
287381148e9c24687564b3dd13bcb5e5e9f5100c0c95Fariborz Jahanian      if (CXXRecordDecl *CXXRD =
287481148e9c24687564b3dd13bcb5e5e9f5100c0c95Fariborz Jahanian            dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl))
287538c9ab880b843a128f8d895d3e2e24877e178788Fariborz Jahanian        CXXRD->setEmpty(false);
28761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2877838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition();
2878bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
28791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2880bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2881bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2882bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2883adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike StumpQualType ASTContext::getBlockDescriptorType() {
2884adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
2885adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
2886adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2887adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
2888adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
2889465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
289079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
28915cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
2892adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2893adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
2894adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
2895adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
2896adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
2897adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2898adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
2899adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
2900083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
2901adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
2902adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2903adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
2904adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    FieldDecl *Field = FieldDecl::Create(*this,
2905adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         T,
2906adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
2907adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
2908a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
2909adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
2910adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*Mutable=*/false);
29112888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
2912adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
2913adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
2914adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2915838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
2916adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2917adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
2918adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2919adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
2920adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
2921adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2922adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stumpvoid ASTContext::setBlockDescriptorType(QualType T) {
2923adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const RecordType *Rec = T->getAs<RecordType>();
2924adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  assert(Rec && "Invalid BlockDescriptorType");
2925adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = Rec->getDecl();
2926adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
2927adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2928083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike StumpQualType ASTContext::getBlockDescriptorExtendedType() {
2929083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
2930083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
2931083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2932083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
2933083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
2934465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
293579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
29365cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
2937083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2938083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
2939083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
2940083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
2941083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
2942083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
2943083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
2944083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2945083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
2946083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
2947083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
2948083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
2949083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
2950083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
2951083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2952083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
2953083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    FieldDecl *Field = FieldDecl::Create(*this,
2954083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         T,
2955083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
2956083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
2957a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
2958083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
2959083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*Mutable=*/false);
29602888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
2961083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
2962083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
2963083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2964838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
2965083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2966083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
2967083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2968083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
2969083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
2970083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2971083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stumpvoid ASTContext::setBlockDescriptorExtendedType(QualType T) {
2972083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const RecordType *Rec = T->getAs<RecordType>();
2973083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  assert(Rec && "Invalid BlockDescriptorType");
2974083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = Rec->getDecl();
2975083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
2976083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2977af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stumpbool ASTContext::BlockRequiresCopying(QualType Ty) {
2978af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isBlockPointerType())
2979af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
2980af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (isObjCNSObjectType(Ty))
2981af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
2982af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isObjCObjectPointerType())
2983af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
2984af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
2985af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
2986af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
2987af7b44d847d3e44c43346d508b2e55a6254b6e9dMike StumpQualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
2988af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
2989ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
2990af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
2991ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
2992ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
299338e1627e895cd276186ebf177f75ba5d229c3fd7Mike Stump  //    void *__copy_helper;		// as needed
299438e1627e895cd276186ebf177f75ba5d229c3fd7Mike Stump  //    void *__destroy_help		// as needed
2995af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
2996ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
2997ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
2998af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
2999af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3000af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
30014d0d85c3370f2726c74ba0ece0a5e712830a1d82Fariborz Jahanian  static unsigned int UniqueBlockByRefTypeID = 0;
3002f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3003f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3004f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
3005af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
3006465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
300779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get(Name.str()));
3008af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
3009af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
3010af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3011af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
3012af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3013af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
3014af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3015af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3016af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3017af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3018af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
3019af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3020af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3021af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  const char *FieldNames[] = {
3022af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
3023af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
3024af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
3025af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
3026af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
3027af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
3028af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
3029af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3030af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3031af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
3032af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
3033af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
3034af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3035af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
3036a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3037af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
30382888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3039af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
3040af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
3041af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3042838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3043af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3044af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
3045ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
3046ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3047ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3048ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike StumpQualType ASTContext::getBlockParmType(
3049083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  bool BlockHasCopyDispose,
3050ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  llvm::SmallVectorImpl<const Expr *> &Layout) {
3051ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
3052adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Move up
30534d0d85c3370f2726c74ba0ece0a5e712830a1d82Fariborz Jahanian  static unsigned int UniqueBlockParmTypeID = 0;
3054f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3055f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__block_literal_"
3056f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  << ++UniqueBlockParmTypeID;
3057adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3058465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
305979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get(Name.str()));
30605cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3061adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3062adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    getPointerType(VoidPtrTy),
3063adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    IntTy,
3064adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    IntTy,
3065adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    getPointerType(VoidPtrTy),
3066083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    (BlockHasCopyDispose ?
3067083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump     getPointerType(getBlockDescriptorExtendedType()) :
3068083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump     getPointerType(getBlockDescriptorType()))
3069adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3070adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3071adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3072adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__isa",
3073adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__flags",
3074adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__reserved",
3075adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__FuncPtr",
3076adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__descriptor"
3077adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3078adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3079adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 5; ++i) {
3080ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3081adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3082a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3083ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
30842888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3085ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    T->addDecl(Field);
3086ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  }
3087ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3088ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  for (unsigned i = 0; i < Layout.size(); ++i) {
3089ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    const Expr *E = Layout[i];
3090ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
3091ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    QualType FieldType = E->getType();
3092ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    IdentifierInfo *FieldName = 0;
3093ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    if (isa<CXXThisExpr>(E)) {
3094ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      FieldName = &Idents.get("this");
3095ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3096ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump      const ValueDecl *D = BDRE->getDecl();
3097ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      FieldName = D->getIdentifier();
3098ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      if (BDRE->isByRef())
3099ea1471e0e967548c596a71469702f8846dbaf3c0John McCall        FieldType = BuildByRefType(D->getNameAsCString(), FieldType);
3100ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    } else {
3101ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      // Padding.
3102ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      assert(isa<ConstantArrayType>(FieldType) &&
3103ea1471e0e967548c596a71469702f8846dbaf3c0John McCall             isa<DeclRefExpr>(E) &&
3104ea1471e0e967548c596a71469702f8846dbaf3c0John McCall             !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3105ea1471e0e967548c596a71469702f8846dbaf3c0John McCall             "doesn't match characteristics of padding decl");
3106ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    }
3107ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3108ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3109ea1471e0e967548c596a71469702f8846dbaf3c0John McCall                                         FieldName, FieldType, /*TInfo=*/0,
3110ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
31112888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3112adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3113adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3114adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3115838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3116ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3117ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  return getPointerType(getTagDeclType(T));
3118adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3119adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3120319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
31216217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3122319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3123319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3124319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3125319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
3126e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
3127e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
31282d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
3129e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
3130bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3131bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
31321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
313385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
313485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
313585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3136a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
313733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
3138aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen DyckCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
3139199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
31401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
314133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
3142199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  if (sz.isPositive() && type->isIntegralType())
3143199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
314433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
314533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
3146199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
3147aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
3148199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
3149199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
3150199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
3151199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
3152199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
315333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
315433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
31556f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian/// getObjCEncodingForBlockDecl - Return the encoded type for this block
31565e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
31575e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnallvoid ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
31585e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall                                             std::string& S) {
31595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
31605e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
31615e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
31625e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
3163c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
31645e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
31655e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
31665e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
31675e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
3168199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3169199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
31706f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
31715e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
31725e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
3173aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3174199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
31755e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
31765e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
31775e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
3178199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
31795e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
31805e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
31815e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
31825e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
31835e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
31845e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
31855e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
31865e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
31875e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
31885e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
31895e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
31905e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
31915e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
31925e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
31935e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
31945e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
31955e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
31965e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
31975e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
3198199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3199aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
32005e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
32015e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
32025e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
3203a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
320433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
32051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
3206e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
3207c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3208ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
3209a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
321033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
32110d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
321233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
321333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
321433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
321533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
3216199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
321733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
321833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
3219199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
322089951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
32217732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
322289951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
3223aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3224199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
3225199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
322633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
322733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
3228199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
322933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
3230199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
32311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
323233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
323333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
323489951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
32357732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
323689951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
32371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
32384306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
3239ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3240ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
3241ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
3242bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
3243ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
3244ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
3245ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
3246ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
324733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
32484306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
32490d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
3250199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3251aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
325233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
325333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
325433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
3255c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
325683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
3257c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3258c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
32591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
32601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
32611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
32621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
32631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
326483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
326583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
326683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
326783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
326883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
326983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
327083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
327183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
327283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
327383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
327483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
327583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
327683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
327783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
327883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
327983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
32801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
3281c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
3282e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
3283c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
3284c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
3285c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
3286c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3287c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
3288c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
32891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
3290c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
3291c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
329217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
3293653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
3294c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
3295c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
3296c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3297c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
3298c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
3299c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
3300c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
3301c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
3302c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
3303c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
330461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
3305c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
330617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
3307653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
3308c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
3309c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
3310c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3311c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
3312c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
3313c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
3314c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
3315c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
33161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3317c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
3318c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3319c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3320c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3321c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
3322c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3323c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
3324090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
3325090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
33261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
3327090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
3328090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
3329c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3330c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
3331c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
3332c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
3333c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
3334c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
3335c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
33361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
3337c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
3338c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3339c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3340c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
3341c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
3342c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
3343c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
3344c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3345090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3346090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
33471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3348c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3349c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
3350077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
3351c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3352c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3353c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3354c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
3355077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
3356c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3357c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3358c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
3359c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3360c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
336139f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
3362c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3363c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3364c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
3365c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
3366c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3367a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
33681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
33691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
3370a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3371a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
3372a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
33738e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
3374183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
3375c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
3376c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
3377a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
33781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
3379c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
3380c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
3381a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
3382a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
3383a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
3384a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
3385a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
33867d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
3387153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
338882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
338982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
339082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
339182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
33921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
33935b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
339482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
339582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
339664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnallstatic char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
339764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    switch (T->getAs<BuiltinType>()->getKind()) {
339864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    default: assert(0 && "Unhandled builtin type kind");
339964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
340064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
340164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
340264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
340364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
340464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
340564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
340664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall        return
340764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall          (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
340864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
340964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
341064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
341164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
341264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
341364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
341464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
341564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return
341664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall        (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
341764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
341864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
341964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
342064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
342164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongDouble: return 'd';
342264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
342364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
342464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
34251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void EncodeBitField(const ASTContext *Context, std::string& S,
342664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
34278b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
34288b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
34298b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
34308b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
343164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
343264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
343364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
343464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
343564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
343664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
343764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
343864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
343964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
344064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
344164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
344264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
344364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
344464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
344564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
344664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (!Ctx->getLangOptions().NeXTRuntime) {
344764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
344864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
344964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    // FIXME: This same linear search is also used in ExprConstant - it might
345064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    // be better if the FieldDecl stored its offset.  We'd be increasing the
345164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    // size of the object slightly, but saving some time every time it is used.
345264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    unsigned i = 0;
345364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    for (RecordDecl::field_iterator Field = RD->field_begin(),
345464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                                 FieldEnd = RD->field_end();
345564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall         Field != FieldEnd; (void)++Field, ++i) {
345664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      if (*Field == FD)
345764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall        break;
345864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
345964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += llvm::utostr(RL.getFieldOffset(i));
346064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(Context, T);
346164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
346264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
34638b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
34648b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
34658b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
346601eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
346782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
346882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
346982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
3470153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
3471090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
34726ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
347364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (T->getAs<BuiltinType>()) {
3474ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
347564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
347664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(this, T);
3477ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3478ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
34791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3480183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
3481c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
34821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
3483c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
3484ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3485ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
348660bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
3487aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
3488aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
34896217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
34908d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
34918d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
34928d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
34938d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
3494aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
3495aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
3496aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3497aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
3498aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
3499a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
3500a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
3501a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
3502a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
35031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
35048e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
3505a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
3506a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3507a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3508a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
35099fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
3510a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
35116217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
35126217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
3513a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
3514a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3515a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3516a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
3517a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
3518a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
3519a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
3520a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
3521a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
35220237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      if (llvm::StringRef(S).endswith("nr"))
35230237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
3524a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
35251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
352685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
352785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
352885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
3529e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
353085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
353185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
353285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
35336217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
35349533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
35359533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
35369533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
35379533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
35389533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
35399533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
35409533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
35419533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
35429533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
35439533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
35449533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
354585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
354685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
3547a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
3548a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
35491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
355043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
3551ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3552ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3553aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
3554ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
3555ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
3556ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
3557559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
3558559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
3559559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
3560559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
35611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3562559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3563559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
3564559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
35651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3566559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3567559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
3568559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
3569559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
3570559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3571559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
3572559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
35731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3575559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3576559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
3577559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
3578ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3579ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
35801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3581183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
3582c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
3583ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3584ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
35851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35866217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
358782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
3588d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
3589502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
3590502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3591502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
35926fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
35936fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
35946fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
35956fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
35966fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
35976fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            TemplateArgs.getFlatArgumentList(),
35986fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            TemplateArgs.flat_size(),
35996fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            (*this).PrintingPolicy);
36006fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
36016fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
36026fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
3603502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
3604502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
3605502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
36060d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
36077d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
360817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
360917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   FieldEnd = RDecl->field_end();
361044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
361143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
3612d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
361344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
3614d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
3615d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
36161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3617d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
361843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
36191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
362043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
3621d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
3622a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
3623a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
36241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(qt, S, false, true,
362543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
3626d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
36277d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
36286de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
3629d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
3630ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3631ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3633ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
36348b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
363564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      EncodeBitField(this, S, T, FD);
36368b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
36378b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
3638ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3639ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3641ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
364221a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
3643ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3644ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3646c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
3647c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3648c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
3649c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
36500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
365143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
36520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
365343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
365443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
365543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
365643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
3657f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
365843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
3659f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
366043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
36611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
366243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
366343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
36641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
366543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
366643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
366743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
3668ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
366943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
36701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3671183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
367214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
367314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
367414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3675ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
36761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
367827d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
367927d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
368027d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
368114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
368214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3683ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
36841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3685ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
36861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
368714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
368814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
368914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
369014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
369114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
369214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
369367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
369467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
369514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
369614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
369714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
369814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
369914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
370014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
370114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3702ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
37031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3704ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
3705ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
3706ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
3707ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
37081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
3709ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
3710ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
37111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
37121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
3713ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
371414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
371514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
3716ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3717ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
371827d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
3719ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
372027d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
372167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
372267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
3723ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
3724ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
3725ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
37261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3727ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
3728ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3729ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3730ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
37311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3732532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
3733532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
3734532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
3735532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
3736532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall
3737ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
373885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
373985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
37401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
3741ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
3742ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
3743ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
3744ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
3745ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
3746ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
3747ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
3748ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
3749ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
3750ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
3751ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
3752ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
3753ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
3754ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
3755ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
3756ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
3757b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
37581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3759b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
3760b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
3761b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3762ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
3763de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
37647e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
37657e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
3766ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
376713dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = T;
3768b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
3769b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3770ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
3771a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
3772390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
3773390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3774ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
3775de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
37768baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
37778baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
3778a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
37791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
37802198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
37811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3782a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
37832198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
37842198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
37850bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
37860bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
3787eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCallTemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3788eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall                                                   UnresolvedSetIterator End) {
37890bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
37900bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
37917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
37920bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
37930bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
37940bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
37950bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
37960bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
3797eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
37980bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
37990bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
38000bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
38010bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
38020bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
38037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
38047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38050bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
38067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
38077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3808d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
3809d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
38101eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
3811d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                                  bool TemplateKeyword,
38120bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                                                  TemplateDecl *Template) {
3813789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
3814d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
3815d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
38161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3817d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
3818d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
38190bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3820d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
3821d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3822d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3823d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
38241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3825d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
3826d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
3827d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
38287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
38297532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
38301eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
38317532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
38321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
38333b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
38347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
38367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
38377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
38397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
38407532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
38417532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38427532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
38437532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
38447532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38457532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
38467532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
38477532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
38487532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
38497532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
38507532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3851789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
3852789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3853789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
3854789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
38557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
38567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38577532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
38587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
38597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
38607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3861ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
3862ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
3863ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
3864ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3865ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                     OverloadedOperatorKind Operator) {
3866ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
3867ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
3868ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3869ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
3870ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
3871ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3872ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
3873789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
3874789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3875ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3876ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
3877ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
3878ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3879ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3880ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
3881ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3882ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
3883ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3884ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
3885789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3886789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
3887789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3888789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
3889789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
3890ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
3891ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3892ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
3893ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
3894ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
3895ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3896b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
3897d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
3898d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
3899e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
3900b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
3901e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
3902b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
3903b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
3904b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
3905b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
3906b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
3907b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
3908b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
3909b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3910b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
3911b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
3912b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
3913e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  return CanQualType();
3914b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
3915b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3916b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3917b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
3918b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3919b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3920fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
3921fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
3922fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
3923f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
3924fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
3925fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
3926fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3927fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
392840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
3929fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
3930fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
39311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
3932fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
3933fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
39344fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
39354fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
39364fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
39370953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
39380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttrs = Qualifiers::GCNone;
39394fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
39404fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
3941b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
39424fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
39431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // (or pointers to them) be treated as though they were declared
3944a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
39450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GCAttrs == Qualifiers::GCNone) {
394675212ee91313bc1b6dd826d9b173541bc4016539Fariborz Jahanian      if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
39470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        GCAttrs = Qualifiers::Strong;
3948a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
39496217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
3950a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
3951c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
3952c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
3953f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
39540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return Qualifiers::GCNone;
39554fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
3956b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
39574fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
39584fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
39596ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
39606ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
39616ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
3962770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
39631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
39646ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
39656ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
39666ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
3967467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
39686ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
396961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
39706ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
39716ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
39724084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
39734084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
39744084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
39754084c306635b70f37029dca938444e6013f08684Steve Naroff
39764084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
39774084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
39780fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
39790fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian                                                ObjCProtocolDecl *rProto) {
39804084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
398114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
39824084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
39834084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
39844084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
39854084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
39864084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
39874084c306635b70f37029dca938444e6013f08684Steve Naroff}
398814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
39894084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
39904084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
39914084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
39924084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
39934084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
39944084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
39954084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
39964084c306635b70f37029dca938444e6013f08684Steve Naroff}
39974084c306635b70f37029dca938444e6013f08684Steve Naroff
39984084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
39994084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
40004084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
40014084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
40024084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
40031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
40044084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
40054084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
40061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
40074084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
40084084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
40094084c306635b70f37029dca938444e6013f08684Steve Naroff
40104084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
4011183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
40121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40134084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
40141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40154084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
40161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
40174084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
40184084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
40194084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40204084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
40214084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
40224084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
40234084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
40240fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
40254084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
40264084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40274084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40284084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
40294084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
40304084c306635b70f37029dca938444e6013f08684Steve Naroff    }
40311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
40324084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40334084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
40344084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
40354084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
4036de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
4037de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
4038de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
4039de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
40404084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
40414084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
40424084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
40434084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
40444084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
40454084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
40468f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
40478f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
4048de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
40491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
40504084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
40514084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
40524084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40534084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
40544084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
40554084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
40564084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
40570fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
40584084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
40594084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
40604084c306635b70f37029dca938444e6013f08684Steve Naroff          }
40614084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40624084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40634084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
4064de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
4065de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
40661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4067de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
4068de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
40691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40704084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
40714084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
40724084c306635b70f37029dca938444e6013f08684Steve Naroff
40731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
40744084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
40754084c306635b70f37029dca938444e6013f08684Steve Naroff    if (lhsOPT->qual_empty()) {
40764084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
40774084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
40784084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
40794084c306635b70f37029dca938444e6013f08684Steve Naroff             E = rhsQID->qual_end(); I != E; ++I) {
40804084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
40814084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
40824084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
40830fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (lhsID->ClassImplementsProtocol(*I, true)) {
40844084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
40854084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
40864084c306635b70f37029dca938444e6013f08684Steve Naroff          }
40874084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40884084c306635b70f37029dca938444e6013f08684Steve Naroff        if (!match)
40894084c306635b70f37029dca938444e6013f08684Steve Naroff          return false;
40904084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40914084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
40924084c306635b70f37029dca938444e6013f08684Steve Naroff    }
40931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
40944084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
40954084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
40964084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
40974084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
40984084c306635b70f37029dca938444e6013f08684Steve Naroff
40994084c306635b70f37029dca938444e6013f08684Steve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
41004084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
41014084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
41024084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
41034084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
41044084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
41054084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
41064084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
41074084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
41084084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
41094084c306635b70f37029dca938444e6013f08684Steve Naroff        }
41104084c306635b70f37029dca938444e6013f08684Steve Naroff      }
41114084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
41124084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
41134084c306635b70f37029dca938444e6013f08684Steve Naroff    }
41144084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
41154084c306635b70f37029dca938444e6013f08684Steve Naroff  }
41164084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
41174084c306635b70f37029dca938444e6013f08684Steve Naroff}
41184084c306635b70f37029dca938444e6013f08684Steve Naroff
41194084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
41204084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
41214084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
41224084c306635b70f37029dca938444e6013f08684Steve Naroff///
41234084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
41244084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
4125c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
4126c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
4127c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
41284084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
4129c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
4130c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
41314084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
41324084c306635b70f37029dca938444e6013f08684Steve Naroff
4133c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
41341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
41351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
41364084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
41374084c306635b70f37029dca938444e6013f08684Steve Naroff
4138c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
4139c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
41404084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
41411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41424084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
414314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
414414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
4145132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4146132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// for providing type-safty for objective-c pointers used to pass/return
4147132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
4148132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4149132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
4150132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
4151132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
4152132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT) {
4153a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
4154132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
4155132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4156132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
4157132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4158132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
4159132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4160a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
4161132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4162132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
4163132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
4164132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4165132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4166132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4167132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
4168132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
4169132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4170132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian        return false;
4171132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4172132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian        return true;
4173132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4174132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
4175132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
4176132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
4177132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
4178132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
4179132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4180e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
4181e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
4182e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
4183e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
4184e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
4185e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
4186e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
4187e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
4188e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4189e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4190c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
4191c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
4192c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
4193c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
4194e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4195e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4196e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
4197e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
4198e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4199e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
4200432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4201c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
4202c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
4203e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4204e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
4205e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4206e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4207e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
4208e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
4209cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
4210cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
4211e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
4212e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
4213e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
4214e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4215e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
4216432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
4217c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
4218c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
4219432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4220432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
4221432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
4222432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
4223432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
4224e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4225e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
4226e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4227db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
4228db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
4229db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
4230db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
4231db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
4232c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
4233c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
4234c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
4235c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
4236c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4237c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4238c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LDecl || !RDecl)
4239db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
4240db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4241c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  while ((LDecl = LDecl->getSuperClass())) {
4242c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
4243e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
4244c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4245c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4246c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
4247c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
4248c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
4249c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4250c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
4251c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
4252e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
4253db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  }
4254db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4255db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
4256db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
4257db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4258c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4259c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
4260c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
4261c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
4262c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
42636ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
42646ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
4265c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
42666ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
42671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42686ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
42696ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
4270c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
42716ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
42721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42736ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
42746ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
4275c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (RHS->getNumProtocols() == 0)
42766ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
42771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4278c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4279c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
428091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
428191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
428291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
428391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
428491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
4285c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4286c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
42878f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
42888f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
428991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
42908f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
42918f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
429291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
429391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
429491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
429591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
429691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
429791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
429891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
42996ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
43006ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
4301389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4302389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
4303183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4304183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
43051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
430614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
4307389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
430814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
430914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
431014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
4311389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
4312389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
43131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
4314ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
43151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
4316ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
43173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
43180e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  if (getLangOptions().CPlusPlus)
43190e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
43200e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
43213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
43223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
43233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4324132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4325132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
4326132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
4327132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4328132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4329132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                        bool OfBlockPointer) {
4330183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
4331183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
433272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
433372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
43343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
43353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
43363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
4338132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
4339132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (OfBlockPointer)
4340132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4341132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
4342132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian   retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
43433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
43446de8b62ae42818c0bd8878b0d1900e17c7c43e0eFariborz Jahanian  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
434561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
43466de8b62ae42818c0bd8878b0d1900e17c7c43e0eFariborz Jahanian  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
434761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
43486a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
43496a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
43506a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
43516a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
4352264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4353264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
43546a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
43556a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      lbaseInfo.getRegParm();
43566a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
43576a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  if (NoReturn != lbaseInfo.getNoReturn() ||
43586a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      RegParm != lbaseInfo.getRegParm())
43596a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
43606a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  if (NoReturn != rbaseInfo.getNoReturn() ||
43616a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      RegParm != rbaseInfo.getRegParm())
43626a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
4363264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CallingConv lcc = lbaseInfo.getCC();
4364264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CallingConv rcc = rbaseInfo.getCC();
4365ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  // Compatible functions must have compatible calling conventions
436604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isSameCallConv(lcc, rcc))
4367ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    return QualType();
43681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
4370465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4371465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
43723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
43733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
43743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
43763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
43773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
43783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
43803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
43813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
43823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43837fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
43847fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
43857fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
43863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
43873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
43883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
43893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
43903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
4391132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
43923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
43933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
439461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
439561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
439661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
439761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
43983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
43993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
44003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
44013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
44022455636163fdd18581d7fdae816433f886d88213Mike Stump                           lproto->isVariadic(), lproto->getTypeQuals(),
4403264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           false, false, 0, 0,
4404425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                           FunctionType::ExtInfo(NoReturn, RegParm, lcc));
44053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
44063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
44083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
44093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
441072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
44113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
4412465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
44133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
44143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
44153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
44163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
44173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
44183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
44193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
44203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
44213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
4422b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
4423b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
4424b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
4425b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
4426b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
4427b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
44283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
44293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
44303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
44313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
44323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
44343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
44353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
44362d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getNumArgs(), proto->isVariadic(),
4437264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           proto->getTypeQuals(),
4438264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           false, false, 0, 0,
4439425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                           FunctionType::ExtInfo(NoReturn, RegParm, lcc));
44403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
44413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
44433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
4444425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
4445264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getFunctionNoProtoType(retType, Info);
44463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
44473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4448132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4449132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                bool OfBlockPointer) {
445043d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
445143d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
445243d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
44537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
44547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
44550e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
44560e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
44570e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
44583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
44593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
44603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4461f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
44623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
44633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
44643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
4466a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
4467a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
44680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
44690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
44700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
44710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
44720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace())
44730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
44740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
44760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
44770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
44780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
44790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
44800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
44810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
44820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
44830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
44850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
44860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
44880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
44890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
44900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
44910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
44920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
44933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
44940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
44950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
44973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4498852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
4499852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
4500f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
45011adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
45021adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
45031adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
45041adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
45054c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
45064c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
4507a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4508a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
4509a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4510a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
45111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4512c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
4513c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4514c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4515c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
4516213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
4517213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4518213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
45191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45204e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
45214e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
45221adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
45231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
4524842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
4525842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
4526183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
45273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
45283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
4529bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
4530183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
45313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
45323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
4533bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
45341adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
45353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
4536ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
45373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
45384a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
45391adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
454072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
454172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
4542ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
454372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
454472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
454572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
454672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
454772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
454872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
45497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
45507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
455172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
455272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
455372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
455472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
4555c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
455672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
455772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
455872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
455972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
456072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
456172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
456272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
45631adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
45643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
45653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
45666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
45676217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
45683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
45693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
457007d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
457161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
457207d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
457361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
45743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
45753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
4576c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
4577c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
4578c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
45796217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
45806217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
4581132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
4582c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
4583c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4584c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
4585c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4586c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
4587c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
4588c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
45891adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
45903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
45913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
45923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
45933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
45943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
45953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
45963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
45973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
45983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
45993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
460061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
460161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
460261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
460361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
46043bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
46053bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
46063bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
46073bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
46083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
46093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
461061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
461161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
461261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
461361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
46143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
46153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
46163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
46173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
46183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
46193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
46203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
46213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
46223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
46233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
46243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
46253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
46263bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
46273bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
46287e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
46297e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
46303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
46311adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
4632132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
463372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
463472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
46353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
46361adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
46373cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
46383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
463964cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
464064cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
464164cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
46423cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
46435a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
46441c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
46451c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
46463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
464761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
4648c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
4649c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
46505a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
46515a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
4652c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
4653c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
4654c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
46555fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
46565fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
46573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
465861490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
465914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
4660132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
4661132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
4662132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
4663132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>()))
4664132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return LHS;
4665132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
4666132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4667183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4668183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
466914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
467014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
4671bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
4672132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4673ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
467472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
467572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
4676ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
46777192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
46782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
46792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
46802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
46812390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
46822390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
46832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
46842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
46852390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
46862390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
46872390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
46882390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
46892390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
46902390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
46912390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
46922390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
46932390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
46942390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
46952390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
46962390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
46972390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
46982390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
46992390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
47002390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
47012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
47022390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
47032390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
47042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
47052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
47062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->getNumArgs(), FPT->isVariadic(),
47072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->getTypeQuals(),
47082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->hasExceptionSpec(),
47092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->hasAnyExceptionSpec(),
47102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->getNumExceptions(),
47112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->exception_begin(),
47122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  Info);
47132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
47142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
47152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
47162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
47172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
47182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
47202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
47212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
47222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
47232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
47242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
47252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
47262390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
47272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
47292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
47302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
47312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
47322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
47332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
47342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
47352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
47362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
47382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
47392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
47412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
47422390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
47432390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
47442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
47452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
47462390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47472390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
47482390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
47492390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
47502390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
47512390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
47522390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
47532390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
47542390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
47552390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
47562390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
47572390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
47582390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47595426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
4760ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
4761ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
476288054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
4763ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
4764632d772a78db7e2cd9b36f8a22aee49d44486fbfSebastian Redl  if (T->isBooleanType())
4765ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
4766842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (EnumType *ET = dyn_cast<EnumType>(T))
476729a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
4768f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
4769ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
4770ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4771ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
4772ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4773ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
47746a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
47756a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
47766a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
47776a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
477882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson             VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
47796a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
47806a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
47816a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
4782ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
47836a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
47846a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
47856a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
4786ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
4787ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
4788ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
4789ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
4790ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
4791ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
4792ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
4793ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
4794ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
4795ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
4796ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
4797ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
47982df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
47992df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
4800ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
4801ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
4802ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
4803ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
4804ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4805ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
48062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
48072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
48082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
480986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
481086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
481186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
481286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
481386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
481486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
481586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
481686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters.  This returns the resultant type.
48171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
481886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
481986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  bool AllowTypeModifiers = true) {
482086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
482186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
482286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
48231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
482486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the modifiers first.
482586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
482686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
482786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
48281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
482986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
483086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
483186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
483286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
483386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
483486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
483586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
483686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
483786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
483886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
483986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
484086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
484186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
484286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
484386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
484486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
484586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
484686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
48471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
484886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
484986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
485086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
485186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
485286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
485386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
485486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
485586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
485686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
485786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
485886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
485986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
486086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
486186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
486286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
486386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
486486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
486586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
486686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
486786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
486886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
486986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
487086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
487186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
487286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
487386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
487486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
487586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
487686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
487786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
487886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
487986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
488086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
488186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
488286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
488386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
488486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
488586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
488686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
488786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
488886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
488986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
489086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
489186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
489286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
489386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
489486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
489586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
489686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
489786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
489886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
489986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
490086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
490186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
490286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
490386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
490486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
490586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
490686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
490786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
490886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
490986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
491086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
491186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
491286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
491386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
491486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
491586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
491686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
491786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
491886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
491986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
492086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
492186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Type->isArrayType()) {
492286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
492386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    } else {
492486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
492586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
492686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
492786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
492886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
492986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
493086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
49311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
493286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
49331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
493486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
493582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    // FIXME: Don't know what to do about AltiVec.
493682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Type = Context.getVectorType(ElementType, NumElements, false, false);
493786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
493886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
4939d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
4940d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4941d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
4942d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
4943d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  }
49449a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
4945c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
4946c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
4947f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
494886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
494986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
4950fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
49519a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
4952f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
4953782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
4954f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
4955f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
4956f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
4957fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
4958f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
4959fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
4960fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
4961fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
4962782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
49631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
496486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (!AllowTypeModifiers)
496586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return Type;
49661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
496786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Done = false;
496886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
4969187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
497086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      default: Done = true; --Str; break;
497186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '*':
497286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '&':
4973187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        {
4974187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          // Both pointers and references can have their pointee types
4975187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          // qualified with an address space.
4976187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          char *End;
4977187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          unsigned AddrSpace = strtoul(Str, &End, 10);
4978187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          if (End != Str && AddrSpace != 0) {
4979187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall            Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4980187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall            Str = End;
4981187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          }
4982187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        }
4983187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        if (c == '*')
4984187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          Type = Context.getPointerType(Type);
4985187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        else
4986187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          Type = Context.getLValueReferenceType(Type);
498786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
498886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      // FIXME: There's no way to have a built-in with an rvalue ref arg.
498986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case 'C':
49900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Type = Type.withConst();
499186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
4992013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian      case 'D':
4993013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian        Type = Context.getVolatileType(Type);
4994013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian        break;
499586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
499686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
49971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
499886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
499986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
500086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
500186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
500286df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id,
500386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                    GetBuiltinTypeError &Error) {
500486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(id);
50051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
500686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
50071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
500886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
500986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
501086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
501186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
501286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
501386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
501486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
501586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
501686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
501786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
501886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
501986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
50201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
502286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
502386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
502486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
502586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
502686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
502786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
502886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (ArgTypes.size() == 0 && TypeStr[0] == '.')
502986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return getFunctionNoProtoType(ResType);
5030ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
5031ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor  // FIXME: Should we create noreturn types?
503286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
5033ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                         TypeStr[0] == '.', 0, false, false, 0, 0,
5034264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                         FunctionType::ExtInfo());
503586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
5036a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
5037a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType
5038a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
5039a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Perform the usual unary conversions. We do this early so that
5040a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // integral promotions to "int" can allow us to exit early, in the
5041a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // lhs == rhs check. Also, for conversion purposes, we ignore any
5042a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // qualifiers.  For example, "const float" and "float" are
5043a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // equivalent.
5044a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isPromotableIntegerType())
5045a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = getPromotedIntegerType(lhs);
5046a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
5047a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = lhs.getUnqualifiedType();
5048a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (rhs->isPromotableIntegerType())
5049a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = getPromotedIntegerType(rhs);
5050a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
5051a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = rhs.getUnqualifiedType();
5052a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
5053a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If both types are identical, no conversion is needed.
5054a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs == rhs)
5055a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
50561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5057a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5058a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The caller can deal with this (e.g. pointer + int).
5059a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5060a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
50611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // At this point, we have two different arithmetic types.
50631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5064a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Handle complex types first (C99 6.3.1.8p1).
5065a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexType() || rhs->isComplexType()) {
5066a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the complex type.
50671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
5068a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
5069a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5070a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
50711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
5072a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
5073a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5074a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5075a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // This handles complex/complex, complex/float, or float/complex.
50761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // When both operands are complex, the shorter operand is converted to the
50771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // type of the longer, and that is the type of the result. This corresponds
50781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // to what is done when combining two real floating-point operands.
50791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // The fun begins when size promotion occur across type domains.
5080a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // From H&S 6.3.4: When one operand is complex and the other is a real
50811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // floating-point type, the less precise type is converted, within it's
5082a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // real or complex domain, to the precision of the other type. For example,
50831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // when combining a "long double" with a "double _Complex", the
5084a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // "double _Complex" is promoted to "long double _Complex".
5085a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
50861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (result > 0) { // The left side is bigger, convert rhs.
5088a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
50891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (result < 0) { // The right side is bigger, convert lhs.
5090a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
50911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
5092a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // At this point, lhs and rhs have the same rank/size. Now, make sure the
5093a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // domains match. This is a requirement for our implementation, C99
5094a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // does not require this promotion.
5095a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5096a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5097a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return rhs;
5098a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      } else { // handle "_Complex double, double".
5099a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs;
5100a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      }
5101a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5102a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs; // The domain/size match exactly.
5103a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5104a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Now handle "real" floating types (i.e. float, double, long double).
5105a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5106a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the real floating type.
5107a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isIntegerType()) {
5108a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the lhs floating point type.
5109a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5110a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5111a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isComplexIntegerType()) {
5112a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the complex floating point type.
5113a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(lhs);
5114a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5115a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isIntegerType()) {
5116a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the rhs floating point type.
5117a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5118a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
51191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isComplexIntegerType()) {
5120a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the complex floating point type.
5121a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(rhs);
5122a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5123a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // We have two real floating types, float/complex combos were handled above.
5124a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Convert the smaller operand to the bigger result.
5125a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
5126a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (result > 0) // convert the rhs
5127a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5128a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    assert(result < 0 && "illegal float comparison");
5129a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return rhs;   // convert the lhs
5130a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5131a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5132a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Handle GCC complex int extension.
5133a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5134a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5135a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
5136a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhsComplexInt && rhsComplexInt) {
51371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
5138a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman                              rhsComplexInt->getElementType()) >= 0)
5139a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs; // convert the rhs
5140a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5141a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (lhsComplexInt && rhs->isIntegerType()) {
5142a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
5143a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5144a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (rhsComplexInt && lhs->isIntegerType()) {
5145a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
5146a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5147a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5148a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5149a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Finally, we have two differing integer types.
5150a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The rules for this case are in C99 6.3.1.8
5151a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  int compare = getIntegerTypeOrder(lhs, rhs);
5152a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  bool lhsSigned = lhs->isSignedIntegerType(),
5153a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman       rhsSigned = rhs->isSignedIntegerType();
5154a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  QualType destType;
5155a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhsSigned == rhsSigned) {
5156a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Same signedness; use the higher-ranked type
5157a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = compare >= 0 ? lhs : rhs;
5158a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (compare != (lhsSigned ? 1 : -1)) {
5159a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The unsigned type has greater than or equal rank to the
5160a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // signed type, so use the unsigned type
5161a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? rhs : lhs;
5162a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5163a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The two types are different widths; if we are here, that
5164a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // means the signed type is larger than the unsigned type, so
5165a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // use the signed type.
5166a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? lhs : rhs;
5167a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else {
5168a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The signed type is higher-ranked than the unsigned type,
5169a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // but isn't actually any bigger (like unsigned int and long
5170a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // on most 32-bit systems).  Use the unsigned type corresponding
5171a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // to the signed type.
5172a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5173a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5174a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return destType;
5175a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
5176