ASTContext.cpp revision 465d41b92b2c862f3062c412a0538db65c6a2661
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"
212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2219cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
231b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
24a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
26f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
2785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
286fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
29f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
3029445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson#include "RecordLayoutBuilder.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),
47c6fbbedb3e90ff2f04828c36fd839e01468679f5Douglas Gregor  SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
49ac9590effa90406767a544005ed1de52e258306bTed Kremenek  BuiltinInfo(builtins),
50ac9590effa90406767a544005ed1de52e258306bTed Kremenek  DeclarationNames(*this),
51ac9590effa90406767a544005ed1de52e258306bTed Kremenek  ExternalSource(0), PrintingPolicy(LOpts),
520c01d18094100db92d38daa923c95661512db203John McCall  LastSDM(0, 0) {
530f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCIdRedefinitionType = QualType();
540f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCClassRedefinitionType = QualType();
55369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian  ObjCSelRedefinitionType = QualType();
561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
57e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
5814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
59e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
60e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
623478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
633478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
643478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  // Release all of the memory associated with overridden C++ methods.
677d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
687d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor         OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
697d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor       OM != OMEnd; ++OM)
707d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    OM->second.Destroy();
713478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
72bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek  if (FreeMemory) {
73bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    // Deallocate all the types.
74bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    while (!Types.empty()) {
75bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      Types.back()->Destroy(*this);
76bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      Types.pop_back();
77bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    }
78b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
79bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    for (llvm::FoldingSet<ExtQuals>::iterator
80bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek         I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
81bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      // Increment in loop to prevent using deallocated memory.
820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Deallocate(&*I++);
83bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    }
840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
85503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
86503524acc73f2f8280080ca0d200377406001b65Ted Kremenek         I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
87503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      // Increment in loop to prevent using deallocated memory.
88503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
89503524acc73f2f8280080ca0d200377406001b65Ted Kremenek        R->Destroy(*this);
90503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    }
91b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
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
101ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
1021ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
1031ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         NNSEnd = NestedNameSpecifiers.end();
105bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek       NNS != NNSEnd; ) {
106bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    // Increment in loop to prevent using deallocated memory.
1071ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
108bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek  }
109ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
110ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
111ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
112ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
113b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
1182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
1192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
1247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
125dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
127dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
128dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
129dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
130dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
131c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
134dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
137dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
138dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
139dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
140dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
141dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
142dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
143dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
144dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
145dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
147dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
156e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
1576b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
158e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
1596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
17115b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
19364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
1942df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
1952df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
1962df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
1972df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
1983a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
1993a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
2003a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
2013a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
20264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
203f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
204f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
205f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
206f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
207f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
208f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
209f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
210f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
211f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
212f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2138e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
214898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
215898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
216898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
217898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
218898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
219898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
220898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
221898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
2228e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Placeholder type for C++0x auto declarations whose real type has
224e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // not yet been deduced.
225e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
2295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
2318e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2327e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
2331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
234de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
235de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
236de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
23713dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = QualType();
2381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23913dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
240de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
241de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
24213dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
24314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
244a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
2451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
24733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2486e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2496e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2506e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
253251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
254663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
2557caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
256663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
2577caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
2587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
2597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
2601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
2627caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
265251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
266251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor                                                TemplateSpecializationKind TSK) {
2677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
2687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
2697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
2707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
271251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
272251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    = new (*this) MemberSpecializationInfo(Tmpl, TSK);
2737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2757ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
276ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
2777ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
278ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
279ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
2800d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2820d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
2830d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
2840d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
2850d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
286ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
287ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
288ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
289ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
290ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
291ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
292ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
293ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
294ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
295ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
296ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
297ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
298ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
299ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
300ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
301ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
302ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
303ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
304ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
305ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
306ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
307ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
308ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
309ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
3100d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
3110d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
312d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
313d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
314d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
315d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
316d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
318d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
319d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
320d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
321d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
322d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
323d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
324d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
325d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
326d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
3271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
328d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
329d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
330d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
3317d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
3327d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
3337d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
3347d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
3357d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
3367d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
3377d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3387d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
3397d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3407d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3417d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
3427d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
3437d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
3447d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
3457d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
3467d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
3477d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3487d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
3497d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3507d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
3527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
3537d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
3547d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace {
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  class BeforeInTranslationUnit
3582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    : std::binary_function<SourceRange, SourceRange, bool> {
3592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    SourceManager *SourceMgr;
3601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  public:
3622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    bool operator()(SourceRange X, SourceRange Y) {
3652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
3662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  };
3682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
3692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
370464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
371464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
372464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
373a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
374b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
375b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
376b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
377183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
378b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
379b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
380b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
381b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
382b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
383b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
384b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
385b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
386b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
3878b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
388af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
389af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
3905d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
3915d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
3928b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
393dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
394dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
39540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
396bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    Align = std::max(Align, AA->getMaxAlignment());
397dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
398af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
399af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
4006217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
4015d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
4025d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
4035d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
4045d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
4055d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
4065d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
4074cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
408dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
409dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
410dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
411dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
412dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
41305f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
41405f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      // In the case of a field in a packed struct, we want the minimum
41505f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      // of the alignment of the field and the alignment of the struct.
41605f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      Align = std::min(Align,
41705f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis        getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
41805f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
419af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
420dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
4218b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  return CharUnits::fromQuantity(Align / Target.getCharWidth());
422af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
423b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
424a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
425a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
4260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
4270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
4280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
4290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
430d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
4311d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
4325e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
4335e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
434a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
43572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
43672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
43718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
43872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
43972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
44018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
44172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
44272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
4435d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
4445d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
44518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
44618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
44718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
44818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
44918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
45072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
451fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
45218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
45318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
45418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
45518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
456fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
4571d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45998be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
4609e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
461030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
462030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
4635c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
464213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
465030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
4669fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
4679fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
4689fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
4694bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
4706fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
4716fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
4728eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
4739fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
4749fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
4759fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
476030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
477030d8846c7e520330007087e949f621989876e3aChris Lattner  }
4785d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
4799e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
480a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
481692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
482d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
48318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
48418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
48518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
48618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
48718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
4886f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
4899e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
4909e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
4916f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
492692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
493692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
494692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
4956f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
4969e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
4979e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
4986f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
49964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
50064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
50164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
50264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
503f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
504f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
505f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
506f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
507f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
508f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
509f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
510f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
511692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
5126f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
5139e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
5149e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
5156f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
516692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
5176f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
5189e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
5199e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
5206f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
521692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
5226f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
5239e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
5249e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
5256f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
526692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
5276f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
5289e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
5299e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
5306f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
531ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
532ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
533ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
534ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
535ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
5366f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
5379e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
5389e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
5396f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5406f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
5415426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
5425426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
5436f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5446f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
5459e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
5469e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
5476f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5486e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
5496e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
5506e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
5511590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
552a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
553bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
554d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
5555426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
556f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
5576f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
558485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
559485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
560485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
561485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
562485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
563485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
5645d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
5655d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
5665d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
5675d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
5685d484e8cf710207010720589d89602233de61d01Sebastian Redl    unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
5695d484e8cf710207010720589d89602233de61d01Sebastian Redl    Width = Target.getPointerWidth(AS);
5705d484e8cf710207010720589d89602233de61d01Sebastian Redl    Align = Target.getPointerAlign(AS);
5715d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
5725d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
573f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
574f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
5755426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
576f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
577f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
578f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
579f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
580f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
5821cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
5831cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
584f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
585f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
5861cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
5871cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
588f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
5895d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
5905d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
5915d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
59398be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
5949e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
5955d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
5965d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
5975d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
59844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
5991d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
60044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
60144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
60244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
60344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
60444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
60572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
60672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
6071d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
6081d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
6091d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
6108389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
6118389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
6128389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
6138389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6151d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
6167176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
6177176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
6181d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
6197176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
6207176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
6217176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
622dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
623a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
6247532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
6259fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
62649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
62749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
62849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
62918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
63018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
63140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
632bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      Align = std::max(Aligned->getMaxAlignment(),
633bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt                       getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
63418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
63518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
63618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
6377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
6387176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
63918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
64018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
64118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
64218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
64318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
64418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
64518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
64618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
647395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
648395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
649395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
650395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
651465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
652465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
65618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
65718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
65818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
65918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
66018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
66118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
663464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
6649e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
665a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
666a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
667bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
668bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
669bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen DyckCharUnits ASTContext::getTypeSizeInChars(QualType T) {
670199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
671bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
672bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen DyckCharUnits ASTContext::getTypeSizeInChars(const Type *T) {
673199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
674bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
675bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
67616e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
67786fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
67886fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken DyckCharUnits ASTContext::getTypeAlignInChars(QualType T) {
67986fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck  return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
68086fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
68186fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken DyckCharUnits ASTContext::getTypeAlignInChars(const Type *T) {
68286fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck  return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
68386fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
68486fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
68534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
68634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
68734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
68834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
68934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
69034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
6911eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
6921eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
693183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
6941eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
6951eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
6961eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
6971eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
6981eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
69934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
70034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
70134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
702a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
703a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
704a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
705a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
706a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
707f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
708a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
709a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
710a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
711a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
712a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
713a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
714a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
715a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
716a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
717a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
718a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
719a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
7208e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
7218e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
7228e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
7238e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
72411062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
7258e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
7268e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian         E = OI->ivar_end(); I != E; ++I) {
7278e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian     Ivars.push_back(*I);
7288e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
72911062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian
73011062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  CollectNonClassIvars(OI, Ivars);
7318e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
7328e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
73311062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian/// CollectNonClassIvars -
73411062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian/// This routine collects all other ivars which are not declared in the class.
7357d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek/// This includes synthesized ivars (via @synthesize) and those in
7367d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek//  class's @implementation.
7379820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
73811062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanianvoid ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
7399820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
7400e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  // Find ivars declared in class extension.
7410e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
7420e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian    for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
7430e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian         E = CDecl->ivar_end(); I != E; ++I) {
7440e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian      Ivars.push_back(*I);
7450e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian    }
7460e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  }
74711062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian
7487d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek  // Also add any ivar defined in this class's implementation.  This
7497d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek  // includes synthesized ivars.
75011062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
75111062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian    for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
75211062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian         E = ImplDecl->ivar_end(); I != E; ++I)
75311062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian      Ivars.push_back(*I);
75411062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  }
7559820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
7569820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
757e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
758e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
759e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
760432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
761e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
762e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
763e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OI->protocol_end(); P != PE; ++P) {
764e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
765432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
766e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
767b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
768b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian        Protocols.insert(*P);
769e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
770e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
771b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
772e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
773e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
774e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
775e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
776e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
777e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
778e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
779e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
780e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
781e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
782b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
783e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
784e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
785e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
786432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
787e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
788e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
789e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
790e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
791b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
792e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
793e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
794e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
795432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
796e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
797e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
798e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
799e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
800e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
801e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
802e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
8033bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanianunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
8043bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
8053bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
806b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  if (const ObjCCategoryDecl *CDecl = OI->getClassExtension())
807b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
808b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
8093bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
8103bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
8113bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
812b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
813b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
8148e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8158e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8168e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8178a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
8188a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
8198a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8208a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8218a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8228a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
8238a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8248a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8258a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
8268a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
8278a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8288a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8298a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8308a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
8318a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8328a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8338a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
8348a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
8358a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
8368a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
8378a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
8388a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
8398a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8408a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
8418a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
8428a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
8438a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
8448a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
8458a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8468a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
847a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Allocate an uninitialized TypeSourceInfo.
848b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
849a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// The caller should initialize the memory held by TypeSourceInfo using
850b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
851b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
852b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
853b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
854b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
855a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
856109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall                                                 unsigned DataSize) {
857109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
858109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
859109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
860109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
861a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
862109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
863a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
864a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
865a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
866a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
867b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
868b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
869a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
870a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall                                                     SourceLocation L) {
871a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
872a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  DI->getTypeLoc().initialize(L);
873a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
874a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
875a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
876b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the
877b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface.
878b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar///
879b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's
880b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars.
88144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout &
882b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
883b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar                          const ObjCImplementationDecl *Impl) {
884532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar  assert(!D->isForwardDecl() && "Invalid interface decl!");
885532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar
88644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Look up this layout, if already laid out, return what we have.
8871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ObjCContainerDecl *Key =
888d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
889d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
890d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    return *Entry;
89144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
892453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // Add in synthesized ivar count if laying out an implementation.
893453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
8943bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian    unsigned SynthCount = CountNonClassIvars(D);
895d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    // If there aren't any sythesized ivars then reuse the interface
896453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entry. Note we can't cache this because we simply free all
897453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entries later; however we shouldn't look up implementations
898453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // frequently.
8998e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if (SynthCount == 0)
900453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar      return getObjCLayout(D, 0);
901453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  }
902453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
90429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
90529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  ObjCLayouts[Key] = NewEntry;
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  return *NewEntry;
90844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel}
90944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
910b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
911b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
912b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
913b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
914b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
915b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
916b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
917b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
918b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
919b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
92088a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
921464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
922464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
92398be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
924952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  D = D->getDefinition();
9254b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  assert(D && "Cannot get layout of forward declarations!");
9264bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
927464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
928ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // Note that we can't save a reference to the entry because this function
929ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // is recursive.
930ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  const ASTRecordLayout *Entry = ASTRecordLayouts[D];
931464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
9324bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
93429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D);
935ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  ASTRecordLayouts[D] = NewEntry;
9361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9378d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar  if (getLangOptions().DumpRecordLayouts) {
9388d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar    llvm::errs() << "\n*** Dumping AST Record Layout\n";
9398d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar    DumpRecordLayout(D, llvm::errs());
9408d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar  }
9418d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar
9425d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
943464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
944464175bba1318bef7905122e9fda20cff926df78Chris Lattner
945f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlssonconst CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
946952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  RD = cast<CXXRecordDecl>(RD->getDefinition());
947f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  assert(RD && "Cannot get key function for forward declarations!");
948f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson
949f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  const CXXMethodDecl *&Entry = KeyFunctions[RD];
950f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  if (!Entry)
951f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson    Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
952f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  else
953f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson    assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
954f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson           "Key function changed!");
955f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson
956f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  return Entry;
957f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson}
958f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson
959a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
960a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
961a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
962a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
9630953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
9640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  unsigned Fast = Quals.getFastQualifiers();
9650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeFastQualifiers();
9660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
9680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
9690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQuals::Profile(ID, TypeNode, Quals);
9700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  void *InsertPos = 0;
9710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
9720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert(EQ->getQualifiers() == Quals);
9730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType T = QualType(EQ, Fast);
9740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return T;
9750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
9760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9776b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
9780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQualNodes.InsertNode(New, InsertPos);
9790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType T = QualType(New, Fast);
9800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return T;
9810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
9820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9830953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getVolatileType(QualType T) {
9840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanT = getCanonicalType(T);
9850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (CanT.isVolatileQualified()) return T;
9860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
9880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
9890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addVolatile();
9900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
9920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
9930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
994f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
995f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
996f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
997f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
998b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
9990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
10000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
10010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
10050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
10060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
10070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
10080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
10091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1011ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1012ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1013b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
10140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                       Qualifiers::GC GCAttr) {
1015d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1016b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1017d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10194027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  if (T->isPointerType()) {
10206217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
102158f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
10224027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
10234027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
10244027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
10254027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
10261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
10280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
10290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
10330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
10340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
10350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
10360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1039d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1040a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1041264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindolastatic QualType getExtFunctionType(ASTContext& Context, QualType T,
1042264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                        const FunctionType::ExtInfo &Info) {
10430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType ResultType;
104443c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  if (const PointerType *Pointer = T->getAs<PointerType>()) {
104543c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    QualType Pointee = Pointer->getPointeeType();
1046264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    ResultType = getExtFunctionType(Context, Pointee, Info);
104743c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (ResultType == Pointee)
104843c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1049ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1050ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    ResultType = Context.getPointerType(ResultType);
105143c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  } else if (const BlockPointerType *BlockPointer
105243c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor                                              = T->getAs<BlockPointerType>()) {
105343c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    QualType Pointee = BlockPointer->getPointeeType();
1054264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    ResultType = getExtFunctionType(Context, Pointee, Info);
105543c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (ResultType == Pointee)
105643c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1057ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1058ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    ResultType = Context.getBlockPointerType(ResultType);
1059ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor   } else if (const FunctionType *F = T->getAs<FunctionType>()) {
1060264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    if (F->getExtInfo() == Info)
106143c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1062ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
106343c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
1064ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor      ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
1065264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                  Info);
10660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    } else {
106743c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
10680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      ResultType
1069ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor        = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1070ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getNumArgs(), FPT->isVariadic(),
1071ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getTypeQuals(),
1072ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->hasExceptionSpec(),
1073ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->hasAnyExceptionSpec(),
1074ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getNumExceptions(),
1075ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->exception_begin(),
1076264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                  Info);
10770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
107843c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  } else
107943c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    return T;
1080ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1081ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1082ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor}
1083ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1084ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas GregorQualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
1085425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1086264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getExtFunctionType(*this, T,
1087264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                 Info.withNoReturn(AddNoReturn));
1088ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor}
1089ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1090ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas GregorQualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
1091425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1092264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getExtFunctionType(*this, T,
1093264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                            Info.withCallingConv(CallConv));
10942455636163fdd18581d7fdae816433f886d88213Mike Stump}
10952455636163fdd18581d7fdae816433f886d88213Mike Stump
1096425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael EspindolaQualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1097425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1098425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  return getExtFunctionType(*this, T,
1099425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                                 Info.withRegParm(RegParm));
1100425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola}
1101425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1117467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1118f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1122f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11246b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1145467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1146f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1150f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11526b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
11595618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
11605618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
1161296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1162296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
11635618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
11645618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
11655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11675618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
11685618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
11695618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11705618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
11711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
11735618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
11745618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1175467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
11765618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11785618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
11795618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
11805618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1181f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11825618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
11836b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
11846b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
11855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
11865618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
11875618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
11885618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
11895618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
11917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
119254e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
119654e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
12007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
12027c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
120354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
120454e14c4db764c0636160d26c5bbf491637c83a76John McCall
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
120854e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
120954e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
121054e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
12117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
12137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
12147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
12157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
12177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
121954e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
122054e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
12217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
12227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
122354e14c4db764c0636160d26c5bbf491637c83a76John McCall
12247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
12257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
12267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
12287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
12297c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
12307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
12317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
12327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
123354e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
12347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
12367c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
12377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
12397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
124054e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
124154e14c4db764c0636160d26c5bbf491637c83a76John McCall
12427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
12437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
12447c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
124554e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
124654e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
124754e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
12487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
12517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1252f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12556b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
12566b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
12595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1262f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1263f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
1265f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1266f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1267f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1268f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1269f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1270f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1271f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1272f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1273f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1274f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1275f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1276f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1277f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
127887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1279f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1280f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1281f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1282f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1283f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1284f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1285f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
12866b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
12876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1288f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1289f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1290f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1291f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1292f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1294fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
12951eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
129638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1297c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1298c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1299923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1300923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1301587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1302587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
130338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
130438aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
130538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
130638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
13090be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
13137192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
13145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
13175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1319467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!EltTy.isCanonical()) {
13201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1321c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
13225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
13247192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1325f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
13265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13286b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
13296b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
13307192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1335bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1336bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
13377e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
13387e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1339c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
13407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          unsigned EltTypeQuals,
13417e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          SourceRange Brackets) {
1342c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1343c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1344c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
13456b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
13466b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
1347c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1348c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1349c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1350c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1351c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1352c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1353898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1354898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
135504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
13567e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy,
13577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                Expr *NumElts,
1358898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
13597e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                unsigned EltTypeQuals,
13607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                SourceRange Brackets) {
1361cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  assert((!NumElts || NumElts->isTypeDependent() ||
1362cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor          NumElts->isValueDependent()) &&
1363898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1364898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
136504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  void *InsertPos = 0;
1366cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  DependentSizedArrayType *Canon = 0;
1367789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
1368cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1369cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  if (NumElts) {
1370cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // Dependently-sized array types that do not have a specified
1371cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // number of elements will have their sizes deduced from an
1372cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // initializer.
1373cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1374cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor                                     EltTypeQuals, NumElts);
1375cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1376cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1377cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
1378cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
137904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *New;
138004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  if (Canon) {
138104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // We already have a canonical version of this array type; use it as
138204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // the canonical type for a newly-built type.
13836b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
13846b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
13856b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                              NumElts, ASM, EltTypeQuals, Brackets);
138604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  } else {
138704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    QualType CanonEltTy = getCanonicalType(EltTy);
138804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    if (CanonEltTy == EltTy) {
13896b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
13906b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedArrayType(*this, EltTy, QualType(),
13916b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                NumElts, ASM, EltTypeQuals, Brackets);
1392cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1393789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      if (NumElts) {
1394789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        DependentSizedArrayType *CanonCheck
1395789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor          = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1396789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        assert(!CanonCheck && "Dependent-sized canonical array type broken");
1397789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        (void)CanonCheck;
1398cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor        DependentSizedArrayTypes.InsertNode(New, InsertPos);
1399789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      }
140004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    } else {
140104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
140204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  ASM, EltTypeQuals,
140304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  SourceRange());
14046b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
14056b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedArrayType(*this, EltTy, Canon,
14066b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                NumElts, ASM, EltTypeQuals, Brackets);
140704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    }
140804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  }
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1410898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1411898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1412898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1413898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1414c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1415c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1416c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1417c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
14180be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1419c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1420c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
14211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (IncompleteArrayType *ATP =
1422c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1423c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1424c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1425c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1426c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1427c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1428c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1429467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!EltTy.isCanonical()) {
1430f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
14312bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1432c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1433c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1434c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1435c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1436f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14372bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1438c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
14396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  IncompleteArrayType *New = new (*this, TypeAlignment)
14406b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
1441c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1442c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1443c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1444c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1445fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1446fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
144773322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
144873322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
144982287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
145082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson                                   bool IsAltiVec, bool IsPixel) {
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
14521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1453f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
145473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
145882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  VectorType::Profile(ID, vecType, NumElts, Type::Vector,
145982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    IsAltiVec, IsPixel);
14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
146782287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
146882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Canonical = getVectorType(getCanonicalType(vecType),
146982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson      NumElts, false, false);
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1473f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14756b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
147682287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1482213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
148373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1484213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
148573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1487f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1488213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
14891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
149173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
149282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
149373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
149473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
149573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
149673322924127c873c13101b705dd823f5539ffa5fSteve Naroff
149773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
149873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
149973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
1500467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
1501213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
150373322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
150473322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1505f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
150673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
15076b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
15086b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
150973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
151073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
151173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
151273322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
151373322924127c873c13101b705dd823f5539ffa5fSteve Naroff
15141eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
15159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    Expr *SizeExpr,
15169cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    SourceLocation AttrLoc) {
15172ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
15192ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15212ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
15222ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
15232ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
15242ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
15252ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
15262ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
15272ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
15286b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
15296b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
15306b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
15312ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
15322ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
15332ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
15346b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
15356b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
15366b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
1537789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1538789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
1539789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1540789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1541789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
15422ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
15432ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
15442ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
15452ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
15466b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
15476b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
15482ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
15492ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15519cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
15529cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
15539cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15549cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
155572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
15565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1557264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael EspindolaQualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1558264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                            const FunctionType::ExtInfo &Info) {
1559264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  const CallingConv CallConv = Info.getCC();
15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1563264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
156772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
15685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1571ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
157204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
1573264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
1574264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
1575264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
157872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
157972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1580f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15836b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
1584264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    FunctionNoProtoType(ResultTy, Canonical, Info);
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
158672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
159261710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1593971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1594465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     unsigned TypeQuals, bool hasExceptionSpec,
1595465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     bool hasAnyExceptionSpec, unsigned NumExs,
1596264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                     const QualType *ExArray,
1597264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                     const FunctionType::ExtInfo &Info) {
1598264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  const CallingConv CallConv= Info.getCC();
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
160272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1603465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                             TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1604264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                             NumExs, ExArray, Info);
16055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
160872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1610465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1611465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
161254e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
161454e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
16155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
1618465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
162004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
162454e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
1625465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1626f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1627beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
162847259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                isVariadic, TypeQuals, false,
1629264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                false, 0, 0,
1630264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
1631465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
163372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
163472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1635f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1637465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
163872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1639465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
1640465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // end of them.
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionProtoType *FTP =
1642465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1643465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumArgs*sizeof(QualType) +
16446b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                 NumExs*sizeof(QualType), TypeAlignment);
164572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1646465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                              TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1647264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                              ExArray, NumExs, Canonical, Info);
16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
164972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
16543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
16553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
16563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
16573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
16583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
16593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
16603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
16613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
16623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
16633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
16643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
16653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
16663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
16673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
16683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
16693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              QualType TST) {
16703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
16713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
16723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
16733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else if (CXXRecordDecl *PrevDecl
16743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall               = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
16753cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
16763cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
16773cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
16783cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
167931f17ecbef57b5679c017c375db330546b7b5145John McCall    Decl->TypeForDecl =
168031f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
16813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Types.push_back(Decl->TypeForDecl);
16823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
16833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
16843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
16853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
16862ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
16872ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
1688becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCallQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
16891e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
1690becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
16911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169219c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
16932ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1694becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
1695becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  if (const ObjCInterfaceDecl *ObjCInterface
16969fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump               = dyn_cast<ObjCInterfaceDecl>(Decl))
16972ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getObjCInterfaceType(ObjCInterface);
169849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1699becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
1700becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
1701becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
170219c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1703becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Record->getPreviousDeclaration() &&
1704becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
1705becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
1706becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
170719c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1708becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Enum->getPreviousDeclaration() &&
1709becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
1710becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
171119c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
1712ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1713ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
17149fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
1715becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
171649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1717becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  Types.push_back(Decl->TypeForDecl);
171849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
17192ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
17202ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
17225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
172319c8576b7328f4dc2d07682f5da552875c1912efJohn McCallQualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
17245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
17251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1726f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
17276b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Decl->TypeForDecl = new(*this, TypeAlignment)
17286b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
17295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
173349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
173449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
173549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
173649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                                         QualType Replacement) {
1737467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
173849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
173949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
174049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
174149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
174249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
174349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
174449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
174549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
174649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
174749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
174849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
174949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
175049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
175149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
175249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
175349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
175449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
175549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
1756fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
17571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
175876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
17591eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
176076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
1761fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1762fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
176376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
1764fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
17651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
1766fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1767fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1768fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1769fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
17701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
177176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  if (Name) {
177276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
17736b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
17746b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
1775789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1776789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
1777789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1778789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
1779789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
178076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
17816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
17826b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
1783fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1784fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1785fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1786fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1787fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1788fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1789fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
17903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
17913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
17923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
17933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
17943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              QualType CanonType) {
17953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
17963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
17973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
17983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
17993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
18003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
18013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
18023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
18033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
18043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
18053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
18063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
18073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
18081eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
18097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
1810d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
181131f17ecbef57b5679c017c375db330546b7b5145John McCall                                          QualType Canon,
181231f17ecbef57b5679c017c375db330546b7b5145John McCall                                          bool IsCurrentInstantiation) {
1813d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
1814d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1815833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  llvm::SmallVector<TemplateArgument, 4> ArgVec;
1816833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
1817833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
1818833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
1819833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
182031f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
182131f17ecbef57b5679c017c375db330546b7b5145John McCall                                       Canon, IsCurrentInstantiation);
1822833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
1823833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1824833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
1825833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
18267532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
18277532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
182831f17ecbef57b5679c017c375db330546b7b5145John McCall                                          QualType Canon,
182931f17ecbef57b5679c017c375db330546b7b5145John McCall                                          bool IsCurrentInstantiation) {
1830b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  if (!Canon.isNull())
1831b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    Canon = getCanonicalType(Canon);
1832b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  else {
183331f17ecbef57b5679c017c375db330546b7b5145John McCall    assert(!IsCurrentInstantiation &&
183431f17ecbef57b5679c017c375db330546b7b5145John McCall           "current-instantiation specializations should always "
183531f17ecbef57b5679c017c375db330546b7b5145John McCall           "have a canonical type");
183631f17ecbef57b5679c017c375db330546b7b5145John McCall
1837b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    // Build the canonical template specialization type.
18381275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateName CanonTemplate = getCanonicalTemplateName(Template);
18391275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::SmallVector<TemplateArgument, 4> CanonArgs;
18401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    CanonArgs.reserve(NumArgs);
18411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
18421275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
18431275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
18441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // Determine whether this canonical template specialization type already
18451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // exists.
18461275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::FoldingSetNodeID ID;
184731f17ecbef57b5679c017c375db330546b7b5145John McCall    TemplateSpecializationType::Profile(ID, CanonTemplate, false,
1848828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                        CanonArgs.data(), NumArgs, *this);
18491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
18501275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    void *InsertPos = 0;
18511275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateSpecializationType *Spec
18521275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
18531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    if (!Spec) {
18551275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // Allocate a new canonical template specialization type.
18561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      void *Mem = Allocate((sizeof(TemplateSpecializationType) +
18571275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                            sizeof(TemplateArgument) * NumArgs),
18586b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                           TypeAlignment);
185931f17ecbef57b5679c017c375db330546b7b5145John McCall      Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
18601275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                                  CanonArgs.data(), NumArgs,
1861b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                                                  Canon);
18621275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Types.push_back(Spec);
18631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
18641275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
18651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1866b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    if (Canon.isNull())
1867b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor      Canon = QualType(Spec, 0);
18681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(Canon->isDependentType() &&
18691275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           "Non-dependent template-id type must have a canonical type");
1870b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  }
1871fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
18721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
18731275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
18741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
18751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
187640808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
18776b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
18781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
187931f17ecbef57b5679c017c375db330546b7b5145John McCall    = new (Mem) TemplateSpecializationType(*this, Template,
188031f17ecbef57b5679c017c375db330546b7b5145John McCall                                           IsCurrentInstantiation,
188131f17ecbef57b5679c017c375db330546b7b5145John McCall                                           Args, NumArgs,
1882828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                           Canon);
18831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
188455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
18851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
188655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
188755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
18881eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1889465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
1890465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
1891465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              QualType NamedType) {
1892e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1893465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
1894e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1895e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
1896465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1897e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1898e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1899e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1900789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
1901789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
1902789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
1903465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1904465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
1905789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
1906789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
1907789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1908465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
1909e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1910465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
1911e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1912e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1913e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
19144a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
19154a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
19164a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
19174a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          QualType Canon) {
1918d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1919d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1920d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1921d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
19224a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
19234a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
19244a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
19254a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
19264a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
19274a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
1928d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1929d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1930d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
19314a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
1932d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1933d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
19344714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
19354714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1936d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1937d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1938d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
19394a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
1940d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
19414714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
19421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
1943d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1944d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
19451eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
19464a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
19474a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
19484a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 const TemplateSpecializationType *TemplateId,
19494a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 QualType Canon) {
19501734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
19511734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
1952789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
19534a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
1954789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1955789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
19564714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
19574714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1958789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
1959789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
1960789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
19611734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
19621734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
19631734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
19644a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
19654a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
19664a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
19674a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword ||
19684a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor        CanonType != QualType(TemplateId, 0)) {
19691734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
1970183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = CanonType->getAs<TemplateSpecializationType>();
19711734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
19721734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
19734a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
19741734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
19751734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
19764714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    DependentNameType *CheckT
19774714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor      = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1978789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
1979789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
19801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
19814a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
19821734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
19834714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
19841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
19851734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
19861734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
198788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
198888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
198988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
199088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
19912e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
199288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
199388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
199454e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
199554e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
199654e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
199754e14c4db764c0636160d26c5bbf491637c83a76John McCall
199854e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
199954e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
200054e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
200154e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
200254e14c4db764c0636160d26c5bbf491637c83a76John McCall}
200354e14c4db764c0636160d26c5bbf491637c83a76John McCall
200454e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
200588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
200688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
20071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
200888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
200988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
201088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
201188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
201288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
201388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
201488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
201588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2016d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2017d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// the given interface decl and the conforming protocol list.
201814108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
20191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              ObjCProtocolDecl **Protocols,
2020a42286486c85402c65f9d30df17e6b1b037a6adeFariborz Jahanian                                              unsigned NumProtocols,
2021a42286486c85402c65f9d30df17e6b1b037a6adeFariborz Jahanian                                              unsigned Quals) {
2022d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  llvm::FoldingSetNodeID ID;
202314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
2024a42286486c85402c65f9d30df17e6b1b037a6adeFariborz Jahanian  Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
2025d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2026d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
2027d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (ObjCObjectPointerType *QT =
2028d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2029a42286486c85402c65f9d30df17e6b1b037a6adeFariborz Jahanian    return getQualifiedType(QualType(QT, 0), Qs);
2030d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
203154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Sort the protocol list alphabetically to canonicalize it.
203254e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
203354e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!InterfaceT.isCanonical() ||
203454e14c4db764c0636160d26c5bbf491637c83a76John McCall      !areSortedAndUniqued(Protocols, NumProtocols)) {
203554e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!areSortedAndUniqued(Protocols, NumProtocols)) {
20360237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
20370237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
203854e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
203954e14c4db764c0636160d26c5bbf491637c83a76John McCall
204054e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
204154e14c4db764c0636160d26c5bbf491637c83a76John McCall
204254e14c4db764c0636160d26c5bbf491637c83a76John McCall      Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
204354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                           &Sorted[0], UniqueCount);
204454e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
204554e14c4db764c0636160d26c5bbf491637c83a76John McCall      Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
204654e14c4db764c0636160d26c5bbf491637c83a76John McCall                                           Protocols, NumProtocols);
204754e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
204854e14c4db764c0636160d26c5bbf491637c83a76John McCall
204954e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
205054e14c4db764c0636160d26c5bbf491637c83a76John McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
205154e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
205254e14c4db764c0636160d26c5bbf491637c83a76John McCall
2053fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  // No match.
2054fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  unsigned Size = sizeof(ObjCObjectPointerType)
2055fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor                + NumProtocols * sizeof(ObjCProtocolDecl *);
2056fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2057fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2058fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor                                                                 InterfaceT,
2059fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor                                                                 Protocols,
2060fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor                                                                 NumProtocols);
20611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2062d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  Types.push_back(QType);
2063d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
2064a42286486c85402c65f9d30df17e6b1b037a6adeFariborz Jahanian  return getQualifiedType(QualType(QType, 0), Qs);
2065d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
206688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2067c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// getObjCInterfaceType - Return the unique reference to the type for the
2068c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// specified ObjC interface decl. The list of protocols is optional.
2069c15cb2af27514ecc879daba9aa01389c5203685dSteve NaroffQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
2070a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                       ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
20714b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2072c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
20731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20744b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2075c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (ObjCInterfaceType *QT =
2076c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff      ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
20774b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
20781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
207954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Sort the protocol list alphabetically to canonicalize it.
208054e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
208154e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
20820237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer    llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
20830237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                   Protocols + NumProtocols);
20844b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
208554e14c4db764c0636160d26c5bbf491637c83a76John McCall    unsigned UniqueCount = NumProtocols;
208654e14c4db764c0636160d26c5bbf491637c83a76John McCall    SortAndUniqueProtocols(&Sorted[0], UniqueCount);
208724fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
208854e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
208954e14c4db764c0636160d26c5bbf491637c83a76John McCall
209054e14c4db764c0636160d26c5bbf491637c83a76John McCall    ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
209154e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
209254e14c4db764c0636160d26c5bbf491637c83a76John McCall
2093fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  unsigned Size = sizeof(ObjCInterfaceType)
2094fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor    + NumProtocols * sizeof(ObjCProtocolDecl *);
2095fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2096fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2097fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor                                        const_cast<ObjCInterfaceDecl*>(Decl),
2098fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor                                                         Protocols,
2099fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor                                                         NumProtocols);
210024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
210124fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
210254e14c4db764c0636160d26c5bbf491637c83a76John McCall  ObjCInterfaceTypes.InsertNode(QType, InsertPos);
210324fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
210424fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
210524fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
210672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
210772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
21089752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
21109752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
211172564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
2112dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2113b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2114b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2115b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
21161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2117b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2118b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2119b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2120b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2121b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2122b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
21236b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
2124b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
2125b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2126b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
2127b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
21286b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
21296b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
2130b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2131b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2132b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2133b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2134dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
21356b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
2136dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
21379752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
21389752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2139d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2140d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
21419752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
21429752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
21439752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
21441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
21459752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
2146d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
2147f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
21486b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
21499752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
21509752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2151d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2152d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
215360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
215460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
215560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
2156a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2157a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
216060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
216160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
216260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
216360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
216460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
216560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
216660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
216760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
216860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
216960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
217060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
217160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
217260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
217360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
21741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
217560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
21761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
217860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
217960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (e->isLvalue(Context) == Expr::LV_Valid)
218060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
21811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
218260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
218360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
218460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2185395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2186395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2187395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
21881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2189395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
2190395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) {
2191dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
21929d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  if (e->isTypeDependent()) {
21939d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
21949d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
21951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21969d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
21979d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
21989d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
21999d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
22009d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
22019d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
22026b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
22039d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
22049d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
22059d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
22069d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
22076b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
22089d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
22099d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
22109d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
22119d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2212dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
22136b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
2214dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2215395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2216395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2217395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2218395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
2221e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) {
2222d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2223e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2224e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2225e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
22265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
22291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
22301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
2231a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
2232b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
22335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
223564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
223664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
223764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
223864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
223964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
224064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
224164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
224264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
224364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
224464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
224564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
224664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
224764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
224864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
22498b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
22508b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
22518b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2252b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
22538b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
22548b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2255e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2256e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2257e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2258e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
225954e14c4db764c0636160d26c5bbf491637c83a76John McCallCanQualType ASTContext::getCanonicalParamType(QualType T) {
226054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
226154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
226254e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
226354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
226454e14c4db764c0636160d26c5bbf491637c83a76John McCall
226554e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
226654e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
226754e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
226854e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
226954e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
227054e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
227154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
227254e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
227354e14c4db764c0636160d26c5bbf491637c83a76John McCall
227454e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
227554e14c4db764c0636160d26c5bbf491637c83a76John McCall}
227654e14c4db764c0636160d26c5bbf491637c83a76John McCall
227777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
227877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
227977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
228077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
228177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
228277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
228350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) {
22840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
22850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ptr = Quals.strip(T);
22860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanType = Ptr->getCanonicalTypeInternal();
22870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
22880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // The canonical internal type will be the canonical type *except*
22890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // that we push type qualifiers down through array types.
22901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If there are no new qualifiers to push down, stop here.
22920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!Quals.hasQualifiers())
229350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType);
2294c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
22950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the type qualifiers are on an array type, get the canonical
22960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type of the array with the qualifiers applied to the element
22970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type.
2298c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
2299c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
23000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
23011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2302c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2303c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
23040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
2305c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
23061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2307c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
230850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
230950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getConstantArrayType(NewEltTy, CAT->getSize(),
231050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getSizeModifier(),
23110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                  CAT->getIndexTypeCVRQualifiers()));
2312c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
231350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
231450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
23150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    IAT->getIndexTypeCVRQualifiers()));
23161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2317898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
231850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
231950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getDependentSizedArrayType(NewEltTy,
2320bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                        DSAT->getSizeExpr() ?
2321bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                          DSAT->getSizeExpr()->Retain() : 0,
232250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getSizeModifier(),
23230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                        DSAT->getIndexTypeCVRQualifiers(),
232487a924e37dc40c6e3e2b33d1dbd646aeccf4c15bDouglas Gregor                        DSAT->getBracketsRange())->getCanonicalTypeInternal());
2325898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2326c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
232750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
2328bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                        VAT->getSizeExpr() ?
2329bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr()->Retain() : 0,
233050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                        VAT->getSizeModifier(),
23310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
233250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                     VAT->getBracketsRange()));
2333c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
2334c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
233528e318cc6008c2bc008f0caee70dc736a03d6289Chandler CarruthQualType ASTContext::getUnqualifiedArrayType(QualType T,
233628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                             Qualifiers &Quals) {
23375535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  Quals = T.getQualifiers();
233828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  if (!isa<ArrayType>(T)) {
23395535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth    return T.getUnqualifiedType();
234028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
234128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
234228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  const ArrayType *AT = cast<ArrayType>(T);
234328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  QualType Elt = AT->getElementType();
2344c1ae0a8b0022a9fbb50d3413139f36ec5d8a63beZhongxing Xu  QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
234528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  if (Elt == UnqualElt)
234628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return T;
234728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
234828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
234928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return getConstantArrayType(UnqualElt, CAT->getSize(),
235028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
235128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
235228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
235328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
235428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
235528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
235628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
235728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
235828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
235928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
236028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
236128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
236228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
236380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCallDeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
236480ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (TemplateDecl *TD = Name.getAsTemplateDecl())
236580ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    return TD->getDeclName();
236680ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
236780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
236880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
236980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall      return DeclarationNames.getIdentifier(DTN->getIdentifier());
237080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
237180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall      return DeclarationNames.getCXXOperatorName(DTN->getOperator());
237280ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
237380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
237480ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
23750bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
23760bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(Storage);
23770bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return (*Storage->begin())->getDeclName();
237880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
237980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
238025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
238125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
238225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
238325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
238497fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
238525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
23860bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(!Name.getAsOverloadedTemplate());
23871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
238825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
238925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
239025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
239125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
239225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
2393db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2394db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
2395db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
2396db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
2397db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
2398db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
23991eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
24001275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
24011275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
24021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
24031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
24041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24051275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
24061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
24071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
2409833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
24101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2411788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
2412788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2413788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
24141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
2415833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(*Arg.getAsIntegral(),
24161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
24171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
2419833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
24201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24211275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
24221275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Allocate in ASTContext
24231275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
24241275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
24251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
24261275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
24271275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
24281275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
24291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24301275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument Result;
24311275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
24321275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Result;
24331275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
24341275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
24351275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
24361275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
24371275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
24381275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
24391275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
24401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
2441d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
2442d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
24431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
2444d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
2445d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2446d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
2447d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
2448d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
24491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
2450d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2451d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
2452d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2453d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
2454d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
2455d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
2456d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2457d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2458d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
2459d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
2460d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
24611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this, 0,
24621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2463d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
2464d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2465d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2466d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
2467d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
2468d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
2469d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2470d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2471d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
2472d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
2473d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2474d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2475c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2476c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
2477c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
2478a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
2479c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
2480c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2481c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
2482c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
24831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
2485c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
24860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!isa<ArrayType>(CType))
2487c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
24881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
2490c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
2491c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
24921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2493c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
2494c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
249550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
24960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
24970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
24980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ty = Qs.strip(T.getDesugaredType());
24991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2500c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
2501c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
25020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ATy == 0 || Qs.empty())
2503c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
25041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2505c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
2506c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
2507c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2508c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
25090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
25101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2511c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2512c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2513c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
25140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
2515c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2516c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2517c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
25180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
2519898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
25201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
2521898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
2522898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
25231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
2524bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                DSAT->getSizeExpr() ?
2525bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              DSAT->getSizeExpr()->Retain() : 0,
2526898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
25270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
25287e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
25291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2530c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
25317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
2532bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr() ?
25330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getSizeExpr()->Retain() : 0,
2534c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
25350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
25367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
253777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
253877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
253977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
2540e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
2541e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
2542e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
2543e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
2544e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
2545e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2546e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
2547c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
2548c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
2549c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
2550c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
2551c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2552c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
25531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2554c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
2555e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2556e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
25570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
2558e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
2559e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
25605e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) {
25610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
25620237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer  while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
25630237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer    QT = AT->getElementType();
25640237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer  return Qs.apply(QT);
25655e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
25665e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
2567fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders CarlssonQualType ASTContext::getBaseElementType(const ArrayType *AT) {
2568fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson  QualType ElemTy = AT->getElementType();
25691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2570fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson  if (const ArrayType *AT = getAsArrayType(ElemTy))
2571fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson    return getBaseElementType(AT);
25721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25736183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
25746183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
25756183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
25760de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
25771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
25780de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
25790de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
25800de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
25810de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
25820de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
25830de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
25840de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
25850de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
25860de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
25875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
25885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
2589a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
2590183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
25915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
2592a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2593183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2594183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
2595a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
25965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
25975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
25985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
25995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
26005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
26031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
2604716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
2605716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
26061361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
26071361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
26081361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
26091361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
26101361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
2611716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
2612f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
2613f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
2614f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
2615f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
2616f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
26171361b11066239ea15764a2a844405352d87296b3Chris Lattner
26181361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
26191361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
26201361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
26211361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
26221361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
26231361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
26245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
26255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
26287cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
26297cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
26301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
2631a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2632a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
2633a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
26341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2635a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
2636fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
2637a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
2638fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
2639fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
26405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2642f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2643f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
2644f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
2645f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
2646467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
2647f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
2648842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    T = ET->getDecl()->getPromotionType().getTypePtr();
2649f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2650a34267595534a72703290153a6f7e3da1adcec59Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::WChar))
2651a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
2652a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
2653f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
2654f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2655f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2656f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
2657f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2658f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2659f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
26607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
26617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
2662f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
26637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
26647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
26657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
26667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
2667f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
26687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
26697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
2670f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
26717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
26727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
2673f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
26747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
26757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
2676f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
26777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
26787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
2679f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
26802df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
26812df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
26822df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
2683f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
2684f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
2685f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
268604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
268704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
268804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
268904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
269004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
269104e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) {
269204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
269304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
269404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
269504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
269604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
269704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
269804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
269904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
270004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
270104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
270204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
270304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
270404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
270504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
270604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
270704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
270804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
270904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
271004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
271104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
271204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
271304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
271404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
271504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
271604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
2717a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
2718a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2719a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
2720a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2721a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
2722a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
2723842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
2724842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
2725a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
2726a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
2727a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
2728a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
2729a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2730a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2731a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
2732a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
27331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
27347cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
27351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
27367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
2737f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
2738f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
27397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
27401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2741f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2742f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
27431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
27457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
27461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
27487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
27497cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
27507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
27511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27527cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
27537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
27547cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
27557cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
27567cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
27571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27587cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
27597cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
27601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
27617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
27627cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
27637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
27647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
27657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
27667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
27671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
27697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
27701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
27717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
27725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
277371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
277479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
277579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders CarlssonCreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
277679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                 SourceLocation L, IdentifierInfo *Id) {
277779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  if (Ctx.getLangOptions().CPlusPlus)
277879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson    return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
277979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
278079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson    return RecordDecl::Create(Ctx, TK, DC, L, Id);
278179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
278279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
27831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
278471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
278571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
27861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
2787465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
278879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
27895cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
279079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
2791f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
27921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
279371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
27940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
2795f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2796f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
279771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
27980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
279971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
28001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
28011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
280244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
280344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
28041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
280544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
2806a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
28071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
28084afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
28092888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
281017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
281144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
281244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
2813838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
281471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
28151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
28178467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2818b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2819319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
28206217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2821319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2822319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2823319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2824319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
28252bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian// getNSConstantStringType - Return the type used for constant NSStrings.
28262bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz JahanianQualType ASTContext::getNSConstantStringType() {
28272bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  if (!NSConstantStringTypeDecl) {
28282bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl =
2829465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
28302bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                     &Idents.get("__builtin_NSString"));
28312bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->startDefinition();
28322bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28332bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    QualType FieldTypes[3];
28342bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28352bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const int *isa;
28362bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[0] = getPointerType(IntTy.withConst());
28372bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const char *str;
28382bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[1] = getPointerType(CharTy.withConst());
28392bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // unsigned int length;
28402bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[2] = UnsignedIntTy;
28412bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28422bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // Create fields
28432bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    for (unsigned i = 0; i < 3; ++i) {
28442bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
28452bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           SourceLocation(), 0,
28462bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           FieldTypes[i], /*TInfo=*/0,
28472bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*BitWidth=*/0,
28482bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*Mutable=*/false);
28492888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
28502bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      NSConstantStringTypeDecl->addDecl(Field);
28512bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    }
28522bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28532bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->completeDefinition();
28542bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  }
28552bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28562bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  return getTagDeclType(NSConstantStringTypeDecl);
28572bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
28582bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28592bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanianvoid ASTContext::setNSConstantStringType(QualType T) {
28602bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  const RecordType *Rec = T->getAs<RecordType>();
28612bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  assert(Rec && "Invalid NSConstantStringType");
28622bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  NSConstantStringTypeDecl = Rec->getDecl();
28632bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
28642bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28651eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getObjCFastEnumerationStateType() {
2866bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
286744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
2868465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
286979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__objcFastEnumerationState"));
28705cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    ObjCFastEnumerationStateTypeDecl->startDefinition();
28711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2872bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2873bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2874de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
2875bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2876bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2877bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2878bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
28791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
288044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
28811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this,
28821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           ObjCFastEnumerationStateTypeDecl,
28831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           SourceLocation(), 0,
2884a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
28851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
28864afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
28872888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
288817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
288944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
28901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2891838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition();
2892bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
28931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2894bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2895bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2896bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2897adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike StumpQualType ASTContext::getBlockDescriptorType() {
2898adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
2899adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
2900adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2901adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
2902adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
2903465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
290479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
29055cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
2906adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2907adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
2908adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
2909adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
2910adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
2911adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2912adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
2913adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
2914083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
2915adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
2916adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2917adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
2918adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    FieldDecl *Field = FieldDecl::Create(*this,
2919adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         T,
2920adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
2921adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
2922a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
2923adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
2924adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*Mutable=*/false);
29252888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
2926adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
2927adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
2928adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2929838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
2930adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2931adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
2932adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2933adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
2934adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
2935adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2936adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stumpvoid ASTContext::setBlockDescriptorType(QualType T) {
2937adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const RecordType *Rec = T->getAs<RecordType>();
2938adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  assert(Rec && "Invalid BlockDescriptorType");
2939adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = Rec->getDecl();
2940adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
2941adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2942083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike StumpQualType ASTContext::getBlockDescriptorExtendedType() {
2943083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
2944083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
2945083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2946083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
2947083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
2948465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
294979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
29505cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
2951083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2952083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
2953083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
2954083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
2955083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
2956083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
2957083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
2958083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2959083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
2960083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
2961083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
2962083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
2963083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
2964083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
2965083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2966083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
2967083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    FieldDecl *Field = FieldDecl::Create(*this,
2968083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         T,
2969083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
2970083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
2971a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
2972083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
2973083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*Mutable=*/false);
29742888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
2975083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
2976083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
2977083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2978838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
2979083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2980083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
2981083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2982083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
2983083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
2984083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2985083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stumpvoid ASTContext::setBlockDescriptorExtendedType(QualType T) {
2986083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const RecordType *Rec = T->getAs<RecordType>();
2987083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  assert(Rec && "Invalid BlockDescriptorType");
2988083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = Rec->getDecl();
2989083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
2990083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2991af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stumpbool ASTContext::BlockRequiresCopying(QualType Ty) {
2992af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isBlockPointerType())
2993af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
2994af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (isObjCNSObjectType(Ty))
2995af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
2996af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isObjCObjectPointerType())
2997af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
2998af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
2999af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
3000af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3001af7b44d847d3e44c43346d508b2e55a6254b6e9dMike StumpQualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3002af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
3003ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
3004af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
3005ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
3006ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
300738e1627e895cd276186ebf177f75ba5d229c3fd7Mike Stump  //    void *__copy_helper;		// as needed
300838e1627e895cd276186ebf177f75ba5d229c3fd7Mike Stump  //    void *__destroy_help		// as needed
3009af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
3010ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
3011ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3012af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3013af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3014af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
30154d0d85c3370f2726c74ba0ece0a5e712830a1d82Fariborz Jahanian  static unsigned int UniqueBlockByRefTypeID = 0;
3016f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3017f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3018f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
3019af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
3020465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
302179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get(Name.str()));
3022af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
3023af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
3024af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3025af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
3026af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3027af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
3028af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3029af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3030af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3031af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3032af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
3033af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3034af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3035af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  const char *FieldNames[] = {
3036af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
3037af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
3038af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
3039af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
3040af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
3041af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
3042af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
3043af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3044af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3045af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
3046af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
3047af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
3048af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3049af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
3050a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3051af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
30522888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3053af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
3054af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
3055af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3056838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3057af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3058af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
3059ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
3060ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3061ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3062ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike StumpQualType ASTContext::getBlockParmType(
3063083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  bool BlockHasCopyDispose,
3064ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
3065adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Move up
30664d0d85c3370f2726c74ba0ece0a5e712830a1d82Fariborz Jahanian  static unsigned int UniqueBlockParmTypeID = 0;
3067f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3068f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__block_literal_"
3069f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  << ++UniqueBlockParmTypeID;
3070adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3071465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
307279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get(Name.str()));
30735cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3074adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3075adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    getPointerType(VoidPtrTy),
3076adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    IntTy,
3077adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    IntTy,
3078adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    getPointerType(VoidPtrTy),
3079083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    (BlockHasCopyDispose ?
3080083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump     getPointerType(getBlockDescriptorExtendedType()) :
3081083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump     getPointerType(getBlockDescriptorType()))
3082adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3083adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3084adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3085adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__isa",
3086adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__flags",
3087adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__reserved",
3088adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__FuncPtr",
3089adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__descriptor"
3090adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3091adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3092adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 5; ++i) {
3093ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3094adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3095a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3096ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
30972888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3098ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    T->addDecl(Field);
3099ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  }
3100ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3101ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3102ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    const Expr *E = BlockDeclRefDecls[i];
3103ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3104ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    clang::IdentifierInfo *Name = 0;
3105ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    if (BDRE) {
3106ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump      const ValueDecl *D = BDRE->getDecl();
3107ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump      Name = &Idents.get(D->getName());
3108ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    }
3109ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    QualType FieldType = E->getType();
3110ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3111ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    if (BDRE && BDRE->isByRef())
3112af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3113af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                 FieldType);
3114ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3115ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3116a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         Name, FieldType, /*TInfo=*/0,
3117ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
31182888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3119adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3120adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3121adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3122838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3123ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3124ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  return getPointerType(getTagDeclType(T));
3125adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3126adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3127319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
31286217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3129319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3130319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3131319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3132319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
3133e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
3134e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
31352d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
3136e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
3137bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3138bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
31391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
314085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
314185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
314285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3143a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
314433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
3145aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen DyckCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
3146199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
31471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
314833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
3149199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  if (sz.isPositive() && type->isIntegralType())
3150199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
315133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
315233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
3153199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
3154aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
3155199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
3156199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
3157199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
3158199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
3159199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
316033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
316133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
31626f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian/// getObjCEncodingForBlockDecl - Return the encoded type for this block
31635e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
31645e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnallvoid ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
31655e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall                                             std::string& S) {
31665e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
31675e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
31685e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
31695e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
31705e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
31715e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
31725e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
31735e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
31745e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
3175199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3176199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
31776f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
31785e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
31795e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
3180aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3181199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
31825e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
31835e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
31845e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
3185199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
31865e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
31875e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
31885e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
31895e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
31905e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
31915e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
31925e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
31935e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
31945e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
31955e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
31965e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
31975e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
31985e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
31995e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
32005e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
32015e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
32025e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
32035e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
32045e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
3205199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3206aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
32075e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
32085e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
32095e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
3210a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
321133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
32121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
3213e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
3214c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3215ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
3216a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
321733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
32180d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
321933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
322033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
322133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
322233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
3223199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
322433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
322533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
3226199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
322789951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
32287732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
322989951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
3230aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3231199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
3232199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
323333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
323433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
3235199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
323633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
3237199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
32381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
323933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
324033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
324189951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
32427732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
324389951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
32441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
32454306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
3246ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3247ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
3248ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
3249bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
3250ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
3251ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
3252ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
3253ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
325433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
32554306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
32560d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
3257199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3258aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
325933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
326033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
326133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
3262c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
326383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
3264c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3265c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
32661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
32671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
32681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
32691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
32701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
327183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
327283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
327383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
327483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
327583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
327683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
327783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
327883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
327983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
328083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
328183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
328283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
328383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
328483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
328583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
328683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
32871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
3288c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
3289e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
3290c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
3291c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
3292c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
3293c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3294c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
3295c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
32961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
3297c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
3298c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
329917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
3300653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
3301c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
3302c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
3303c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3304c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
3305c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
3306c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
3307c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
3308c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
3309c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
3310c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
331161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
3312c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
331317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
3314653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
3315c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
3316c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
3317c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3318c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
3319c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
3320c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
3321c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
3322c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
33231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3324c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
3325c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3326c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3327c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3328c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
3329c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3330c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
3331090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
3332090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
33331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
3334090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
3335090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
3336c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3337c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
3338c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
3339c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
3340c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
3341c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
3342c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
33431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
3344c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
3345c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3346c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3347c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
3348c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
3349c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
3350c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
3351c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3352090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3353090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
33541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3355c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3356c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
3357077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
3358c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3359c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3360c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3361c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
3362077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
3363c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3364c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3365c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
3366c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3367c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
336839f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
3369c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3370c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3371c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
3372c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
3373c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3374a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
33751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
33761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
3377a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3378a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
3379a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
33808e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
3381183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
3382c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
3383c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
3384a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
33851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
3386c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
3387c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
3388a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
3389a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
3390a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
3391a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
3392a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
33937d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
3394153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
339582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
339682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
339782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
339882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
33991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
34005b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
340182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
340282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
34031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void EncodeBitField(const ASTContext *Context, std::string& S,
3404153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                           const FieldDecl *FD) {
34058b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
34068b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
34078b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
34089a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
34098b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
34108b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
34118b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
34128b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
341301eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
341482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
341582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
341682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
3417153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
3418090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
34196ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
3420183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
3421ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
3422ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      return EncodeBitField(this, S, FD);
3423ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    char encoding;
3424ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    switch (BT->getKind()) {
34251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: assert(0 && "Unhandled builtin type kind");
3426ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Void:       encoding = 'v'; break;
3427ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Bool:       encoding = 'B'; break;
3428ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_U:
3429ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UChar:      encoding = 'C'; break;
3430ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UShort:     encoding = 'S'; break;
3431ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt:       encoding = 'I'; break;
34321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::ULong:
34331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        encoding =
34341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
343572696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        break;
3436ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt128:    encoding = 'T'; break;
3437ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULongLong:  encoding = 'Q'; break;
3438ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_S:
3439ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::SChar:      encoding = 'c'; break;
3440ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Short:      encoding = 's'; break;
3441ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int:        encoding = 'i'; break;
34421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::Long:
34431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      encoding =
34441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
3445ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      break;
3446ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongLong:   encoding = 'q'; break;
3447ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int128:     encoding = 't'; break;
3448ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Float:      encoding = 'f'; break;
3449ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Double:     encoding = 'd'; break;
3450ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongDouble: encoding = 'd'; break;
345143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
34521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3453ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += encoding;
3454ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3455ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
34561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3457183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
3458c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
34591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
3460c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
3461ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3462ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
346360bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
3464aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
3465aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
34666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
34678d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
34688d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
34698d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
34708d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
3471aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
3472aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
3473aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3474aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
3475aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
3476a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
3477a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
3478a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
3479a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
34801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
34818e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
3482a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
3483a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3484a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3485a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
34869fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
3487a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
34886217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
34896217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
3490a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
3491a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3492a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3493a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
3494a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
3495a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
3496a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
3497a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
3498a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
34990237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      if (llvm::StringRef(S).endswith("nr"))
35000237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
3501a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
35021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
350385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
350485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
350585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
3506e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
350785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
350885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
350985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
35106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
35119533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
35129533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
35139533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
35149533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
35159533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
35169533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
35179533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
35189533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
35199533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
35209533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
35219533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
352285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
352385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
3524a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
3525a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
35261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
352743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
3528ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3529ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3530aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
3531ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
3532ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
3533ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
3534559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
3535559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
3536559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
3537559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
35381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3539559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3540559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
3541559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
35421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3543559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3544559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
3545559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
3546559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
3547559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3548559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
3549559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
35501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3552559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3553559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
3554559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
3555ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3556ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
35571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3558183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
3559c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
3560ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3561ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
35621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
356482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
3565d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
3566502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
3567502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3568502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
35696fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
35706fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
35716fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
35726fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
35736fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
35746fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            TemplateArgs.getFlatArgumentList(),
35756fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            TemplateArgs.flat_size(),
35766fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            (*this).PrintingPolicy);
35776fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
35786fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
35796fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
3580502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
3581502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
3582502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
35830d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
35847d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
358517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
358617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   FieldEnd = RDecl->field_end();
358744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
358843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
3589d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
359044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
3591d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
3592d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
35931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3594d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
359543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
35961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
359743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
3598d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
3599a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
3600a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
36011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(qt, S, false, true,
360243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
3603d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
36047d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
36056de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
3606d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
3607ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3608ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3610ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
36118b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
36128b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
36138b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
36148b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
3615ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3616ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3618ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
361921a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
3620ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3621ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
362443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
36250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
362643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
362743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
362843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
362943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
3630f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
363143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
3632f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
363343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
36341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
363543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
363643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
36371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
363843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
363943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
364043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
3641ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
364243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
36431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3644183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
364514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
364614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
364714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3648ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
36491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
365027d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
365127d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
365227d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
365327d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
365414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
365514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3656ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
36571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3658ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
36591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
366014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
366114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
366214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
366314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
366414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
366514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
366667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
366767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
366814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
366914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
367014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
367114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
367214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
367314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
367414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3675ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
36761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3677ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
3678ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
3679ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
3680ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
36811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
3682ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
3683ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
36841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
36851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
3686ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
368714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
368814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
3689ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3690ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
369127d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
3692ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
369327d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
369467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
369567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
3696ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
3697ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
3698ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
36991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3700ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
3701ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3702ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3703ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
37041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3705ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
370685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
370785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
37081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
3709ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
3710ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
3711ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
3712ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
3713ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
3714ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
3715ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
3716ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
3717ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
3718ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
3719ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
3720ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
3721ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
3722ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
3723ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
3724ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
3725b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
37261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3727b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
3728b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
3729b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3730ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
3731de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
37327e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
37337e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
3734ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
373513dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = T;
3736b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
3737b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3738ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
3739a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
3740390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
3741390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3742ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
3743de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
37448baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
37458baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
3746a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
37471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
37482198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
37491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3750a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
37512198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
37522198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
37530bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
37540bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
3755eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCallTemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3756eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall                                                   UnresolvedSetIterator End) {
37570bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
37580bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
37597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
37600bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
37610bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
37620bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
37630bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
37640bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
3765eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
37660bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
37670bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
37680bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
37690bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
37700bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
37717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
37727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
37730bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
37747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
37757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3776d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
3777d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
37781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
3779d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                                  bool TemplateKeyword,
37800bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                                                  TemplateDecl *Template) {
3781789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
3782d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
3783d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
37841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3785d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
3786d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
37870bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3788d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
3789d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3790d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3791d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
37921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3793d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
3794d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
3795d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
37967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
37977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
37981eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
37997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
38001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
38013b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
38027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
38047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
38057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
38077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
38087532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
38097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
38117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
38127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
38147532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
38157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
38167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
38177532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
38187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3819789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
3820789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3821789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
3822789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
38237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
38247532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38257532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
38267532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
38277532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
38287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3829ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
3830ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
3831ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
3832ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3833ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                     OverloadedOperatorKind Operator) {
3834ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
3835ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
3836ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3837ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
3838ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
3839ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3840ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
3841789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
3842789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3843ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3844ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
3845ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
3846ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3847ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3848ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
3849ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3850ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
3851ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3852ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
3853789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3854789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
3855789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3856789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
3857789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
3858ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
3859ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3860ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
3861ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
3862ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
3863ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3864b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
3865d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
3866d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
3867e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
3868b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
3869e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
3870b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
3871b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
3872b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
3873b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
3874b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
3875b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
3876b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
3877b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3878b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
3879b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
3880b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
3881e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  return CanQualType();
3882b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
3883b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3884b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3885b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
3886b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3887b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3888fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
3889fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
3890fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
3891f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
3892fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
3893fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
3894fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3895fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
389640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
3897fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
3898fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
38991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
3900fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
3901fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
39024fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
39034fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
39044fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
39050953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
39060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttrs = Qualifiers::GCNone;
39074fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
39084fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
3909b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
39104fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
39111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // (or pointers to them) be treated as though they were declared
3912a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
39130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GCAttrs == Qualifiers::GCNone) {
391475212ee91313bc1b6dd826d9b173541bc4016539Fariborz Jahanian      if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
39150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        GCAttrs = Qualifiers::Strong;
3916a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
39176217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
3918a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
3919c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
3920c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
3921f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
39220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return Qualifiers::GCNone;
39234fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
3924b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
39254fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
39264fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
39276ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
39286ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
39296ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
3930770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
39311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
39326ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
39336ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
39346ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
3935467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
39366ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
393761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
39386ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
39396ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
39404084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
39414084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
39424084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
39434084c306635b70f37029dca938444e6013f08684Steve Naroff
39444084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
39454084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
39460fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
39470fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian                                                ObjCProtocolDecl *rProto) {
39484084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
394914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
39504084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
39514084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
39524084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
39534084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
39544084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
39554084c306635b70f37029dca938444e6013f08684Steve Naroff}
395614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
39574084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
39584084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
39594084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
39604084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
39614084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
39624084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
39634084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
39644084c306635b70f37029dca938444e6013f08684Steve Naroff}
39654084c306635b70f37029dca938444e6013f08684Steve Naroff
39664084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
39674084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
39684084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
39694084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
39704084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
39711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
39724084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
39734084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
39741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
39754084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
39764084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
39774084c306635b70f37029dca938444e6013f08684Steve Naroff
39784084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
3979183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
39801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39814084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
39821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39834084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
39841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
39854084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
39864084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
39874084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
39884084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
39894084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
39904084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
39914084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
39920fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
39934084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
39944084c306635b70f37029dca938444e6013f08684Steve Naroff        }
39954084c306635b70f37029dca938444e6013f08684Steve Naroff      }
39964084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
39974084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
39984084c306635b70f37029dca938444e6013f08684Steve Naroff    }
39991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
40004084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40014084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
40024084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
40034084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
4004de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
4005de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
4006de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
4007de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
40084084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
40094084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
40104084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
40114084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
40124084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
40134084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
40148f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
40158f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
4016de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
40171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
40184084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
40194084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
40204084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40214084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
40224084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
40234084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
40244084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
40250fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
40264084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
40274084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
40284084c306635b70f37029dca938444e6013f08684Steve Naroff          }
40294084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40304084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40314084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
4032de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
4033de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
40341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4035de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
4036de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
40371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40384084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
40394084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
40404084c306635b70f37029dca938444e6013f08684Steve Naroff
40411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
40424084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
40434084c306635b70f37029dca938444e6013f08684Steve Naroff    if (lhsOPT->qual_empty()) {
40444084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
40454084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
40464084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
40474084c306635b70f37029dca938444e6013f08684Steve Naroff             E = rhsQID->qual_end(); I != E; ++I) {
40484084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
40494084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
40504084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
40510fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (lhsID->ClassImplementsProtocol(*I, true)) {
40524084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
40534084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
40544084c306635b70f37029dca938444e6013f08684Steve Naroff          }
40554084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40564084c306635b70f37029dca938444e6013f08684Steve Naroff        if (!match)
40574084c306635b70f37029dca938444e6013f08684Steve Naroff          return false;
40584084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40594084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
40604084c306635b70f37029dca938444e6013f08684Steve Naroff    }
40611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
40624084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
40634084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
40644084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
40654084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
40664084c306635b70f37029dca938444e6013f08684Steve Naroff
40674084c306635b70f37029dca938444e6013f08684Steve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
40684084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
40694084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
40704084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
40714084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
40724084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
40734084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
40744084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
40754084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
40764084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
40774084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40784084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40794084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
40804084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
40814084c306635b70f37029dca938444e6013f08684Steve Naroff    }
40824084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
40834084c306635b70f37029dca938444e6013f08684Steve Naroff  }
40844084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
40854084c306635b70f37029dca938444e6013f08684Steve Naroff}
40864084c306635b70f37029dca938444e6013f08684Steve Naroff
40874084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
40884084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
40894084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
40904084c306635b70f37029dca938444e6013f08684Steve Naroff///
40914084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
40924084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
40934084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
40944084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
40954084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
40964084c306635b70f37029dca938444e6013f08684Steve Naroff
40974084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
40981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
40991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
41004084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
41014084c306635b70f37029dca938444e6013f08684Steve Naroff
41024084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
41034084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
41044084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHS && RHS) // We have 2 user-defined types.
41054084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
41061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41074084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
410814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
410914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
4110132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4111132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// for providing type-safty for objective-c pointers used to pass/return
4112132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
4113132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4114132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
4115132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
4116132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
4117132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT) {
4118a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
4119132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
4120132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4121132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
4122132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4123132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
4124132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4125a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
4126132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4127132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
4128132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
4129132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4130132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4131132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4132132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
4133132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
4134132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4135132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian        return false;
4136132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4137132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian        return true;
4138132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4139132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
4140132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
4141132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
4142132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
4143132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
4144132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4145e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
4146e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
4147e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
4148e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
4149e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
4150e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
4151e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
4152e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
4153e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4154e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4155e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4156e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4157e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4158e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4159e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
4160e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
4161e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4162e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
4163432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4164432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
4165e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4166e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
4167e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4168e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4169e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
4170e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
4171cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
4172cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
4173e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
4174e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
4175e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
4176e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4177e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
4178432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
4179e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
4180432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4181432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
4182432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
4183432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
4184432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
4185e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4186e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
4187e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4188db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
4189db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
4190db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
4191db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
4192db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
4193db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian                                          const ObjCObjectPointerType *LHSOPT,
4194db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian                                          const ObjCObjectPointerType *RHSOPT) {
4195db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4196db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4197db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  if (!LHS || !RHS)
4198db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
4199db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4200db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4201db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4202db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    LHS = LHSTy->getAs<ObjCInterfaceType>();
4203e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
4204e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4205e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      getIntersectionOfProtocols(*this,
4206e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                 LHSOPT, RHSOPT, IntersectionOfProtocols);
4207e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (IntersectionOfProtocols.empty())
4208e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        LHSTy = getObjCObjectPointerType(LHSTy);
4209e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      else
4210e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4211e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                                IntersectionOfProtocols.size());
4212e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      return LHSTy;
4213e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
4214db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  }
4215db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4216db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
4217db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
4218db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
42193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
42203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman                                         const ObjCInterfaceType *RHS) {
42216ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
42226ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
42236ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
42246ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
42251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42266ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
42276ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
4228c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
42296ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
42301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42316ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
42326ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
4233c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (RHS->getNumProtocols() == 0)
42346ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
42351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4236c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4237c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                        LHSPE = LHS->qual_end();
423891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
423991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
424091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
424191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
424291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
4243c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
42444084c306635b70f37029dca938444e6013f08684Steve Naroff                                          RHSPE = RHS->qual_end();
42458f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
42468f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
424791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
42488f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
42498f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
425091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
425191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
425291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
425391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
425491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
425591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
425691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
42576ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
42586ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
4259389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4260389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
4261183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4262183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
42631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
426414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
4265389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
426614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
426714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
426814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
4269389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
4270389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
42711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
4272ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
42731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
4274ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
42753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
42760e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  if (getLangOptions().CPlusPlus)
42770e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
42780e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
42793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
42803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
42813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4282132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4283132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
4284132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
4285132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4286132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4287132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                        bool OfBlockPointer) {
4288183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
4289183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
429072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
429172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
42923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
42933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
42943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
42953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
4296132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
4297132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (OfBlockPointer)
4298132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4299132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
4300132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian   retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
43013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
43026de8b62ae42818c0bd8878b0d1900e17c7c43e0eFariborz Jahanian  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
430361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
43046de8b62ae42818c0bd8878b0d1900e17c7c43e0eFariborz Jahanian  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
430561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
43066a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
43076a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
43086a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
43096a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
4310264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4311264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
43126a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
43136a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      lbaseInfo.getRegParm();
43146a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
43156a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  if (NoReturn != lbaseInfo.getNoReturn() ||
43166a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      RegParm != lbaseInfo.getRegParm())
43176a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
43186a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  if (NoReturn != rbaseInfo.getNoReturn() ||
43196a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      RegParm != rbaseInfo.getRegParm())
43206a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
4321264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CallingConv lcc = lbaseInfo.getCC();
4322264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CallingConv rcc = rbaseInfo.getCC();
4323ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  // Compatible functions must have compatible calling conventions
432404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isSameCallConv(lcc, rcc))
4325ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    return QualType();
43261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
4328465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4329465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
43303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
43313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
43323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
43343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
43353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
43363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
43383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
43393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
43403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43417fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
43427fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
43437fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
43443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
43453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
43463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
43473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
43483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
4349132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
43503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
43513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
435261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
435361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
435461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
435561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
43563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
43573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
43583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
43593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
43602455636163fdd18581d7fdae816433f886d88213Mike Stump                           lproto->isVariadic(), lproto->getTypeQuals(),
4361264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           false, false, 0, 0,
4362425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                           FunctionType::ExtInfo(NoReturn, RegParm, lcc));
43633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
43643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
43663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
43673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
436872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
43693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
4370465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
43713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
43723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
43733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
43743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
43753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
43763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
43773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
43783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
43793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
4380b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
4381b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
4382b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
4383b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
4384b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
4385b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
43863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
43873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
43883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
43893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
43903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
43923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
43933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
43942d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getNumArgs(), proto->isVariadic(),
4395264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           proto->getTypeQuals(),
4396264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           false, false, 0, 0,
4397425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                           FunctionType::ExtInfo(NoReturn, RegParm, lcc));
43983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
43993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
44013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
4402425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
4403264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getFunctionNoProtoType(retType, Info);
44043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
44053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4406132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4407132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                bool OfBlockPointer) {
440843d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
440943d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
441043d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
44117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
44127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
44130e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
44140e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
44150e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
44163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
44173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
44183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4419f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
44203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
44213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
44223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
4424a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
4425a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
44260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
44270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
44280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
44290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
44300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace())
44310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
44320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
44340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
44350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
44360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
44370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
44380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
44390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
44400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
44410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
44430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
44440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
44460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
44470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
44480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
44490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
44500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
44513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
44520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
44530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
44540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
44553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4456852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
4457852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
4458f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
44591adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
44601adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
44611adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
44621adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
44634c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
44644c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
4465a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4466a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
4467a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4468a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
44691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4470213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
4471213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4472213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
44731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44744e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
44754e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
44761adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
44771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
4478842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
4479842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
4480183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
44813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
44823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
4483bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
4484183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
44853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
44863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
4487bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
44881adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
44893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
4490ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
44913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44924a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
44931adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
449472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
449572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
4496ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
449772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
449872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
449972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
450072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
450172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
450272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
45037c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
45047c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
450572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
450672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
450772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
450872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
450972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
451072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
451172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
451272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
451372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
451472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
451572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
45161adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
45173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
45183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
45196217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
45206217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
45213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
45223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
452307d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
452461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
452507d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
452661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
45273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
45283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
4529c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
4530c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
4531c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
45326217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
45336217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
4534132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
4535c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
4536c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4537c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
4538c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4539c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
4540c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
4541c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
45421adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
45433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
45443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
45453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
45463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
45473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
45483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
45493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
45503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
45513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
45523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
455361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
455461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
455561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
455661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
45573bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
45583bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
45593bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
45603bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
45613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
45623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
456361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
456461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
456561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
456661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
45673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
45683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
45693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
45703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
45713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
45723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
45733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
45743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
45753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
45763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
45773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
45783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
45793bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
45803bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
45817e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
45827e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
45833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
45841adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
4585132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
458672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
458772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
45883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
45891adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
45903cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
45913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
459264cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
459364cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
459464cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
45953cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
45965a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
45971c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
45981c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
45993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
460061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
460161490e9a965cfee8a78c12c6802138844f04250dCedric Venet  case Type::ObjCInterface: {
46025fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    // Check if the interfaces are assignment compatible.
46035a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
46045a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
4605183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4606183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
46075fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    if (LHSIface && RHSIface &&
46085fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff        canAssignObjCInterfaces(LHSIface, RHSIface))
46095fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
46105fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
46113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
461261490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
461314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
4614132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
4615132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
4616132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
4617132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>()))
4618132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return LHS;
4619132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
4620132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4621183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4622183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
462314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
462414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
4625bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
4626132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4627ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
462872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
462972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
4630ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
46317192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
46325426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
4633ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
4634ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
463588054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
4636ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
4637632d772a78db7e2cd9b36f8a22aee49d44486fbfSebastian Redl  if (T->isBooleanType())
4638ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
4639842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (EnumType *ET = dyn_cast<EnumType>(T))
464029a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
4641f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
4642ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
4643ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4644ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
4645ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4646ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
46476a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
46486a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
46496a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
46506a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
465182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson             VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
46526a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
46536a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
46546a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
4655ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
46566a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
46576a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
46586a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
4659ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
4660ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
4661ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
4662ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
4663ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
4664ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
4665ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
4666ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
4667ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
4668ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
4669ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
4670ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
46712df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
46722df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
4673ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
4674ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
4675ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
4676ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
4677ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4678ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
46792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
46802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
46812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
468286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
468386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
468486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
468586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
468686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
468786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
468886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
468986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters.  This returns the resultant type.
46901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
469186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
469286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  bool AllowTypeModifiers = true) {
469386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
469486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
469586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
46961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
469786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the modifiers first.
469886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
469986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
470086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
47011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
470286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
470386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
470486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
470586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
470686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
470786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
470886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
470986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
471086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
471186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
471286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
471386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
471486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
471586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
471686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
471786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
471886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
471986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
47201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
472186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
472286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
472386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
472486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
472586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
472686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
472786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
472886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
472986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
473086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
473186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
473286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
473386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
473486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
473586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
473686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
473786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
473886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
473986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
474086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
474186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
474286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
474386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
474486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
474586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
474686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
474786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
474886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
474986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
475086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
475186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
475286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
475386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
475486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
475586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
475686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
475786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
475886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
475986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
476086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
476186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
476286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
476386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
476486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
476586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
476686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
476786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
476886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
476986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
477086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
477186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
477286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
477386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
477486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
477586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
477686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
477786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
477886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
477986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
478086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
478186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
478286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
478386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
478486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
478586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
478686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
478786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
478886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
478986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
479086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
479186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
479286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
479386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
479486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Type->isArrayType()) {
479586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
479686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    } else {
479786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
479886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
479986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
480086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
480186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
480286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
480386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
48041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
480586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
48061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
480786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
480882287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    // FIXME: Don't know what to do about AltiVec.
480982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Type = Context.getVectorType(ElementType, NumElements, false, false);
481086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
481186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
4812d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
4813d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4814d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
4815d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
4816d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  }
48179a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
4818c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
4819c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
4820f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
482186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
482286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
4823fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
48249a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
4825f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
4826782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
4827f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
4828f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
4829f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
4830fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
4831f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
4832fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
4833fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
4834fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
4835782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
48361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
483786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (!AllowTypeModifiers)
483886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return Type;
48391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
484086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Done = false;
484186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
4842187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
484386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      default: Done = true; --Str; break;
484486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '*':
484586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '&':
4846187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        {
4847187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          // Both pointers and references can have their pointee types
4848187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          // qualified with an address space.
4849187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          char *End;
4850187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          unsigned AddrSpace = strtoul(Str, &End, 10);
4851187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          if (End != Str && AddrSpace != 0) {
4852187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall            Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4853187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall            Str = End;
4854187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          }
4855187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        }
4856187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        if (c == '*')
4857187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          Type = Context.getPointerType(Type);
4858187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        else
4859187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          Type = Context.getLValueReferenceType(Type);
486086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
486186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      // FIXME: There's no way to have a built-in with an rvalue ref arg.
486286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case 'C':
48630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Type = Type.withConst();
486486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
4865013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian      case 'D':
4866013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian        Type = Context.getVolatileType(Type);
4867013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian        break;
486886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
486986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
48701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
487186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
487286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
487386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
487486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
487586df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id,
487686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                    GetBuiltinTypeError &Error) {
487786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(id);
48781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
487986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
48801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
488186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
488286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
488386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
488486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
488586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
488686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
488786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
488886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
488986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
489086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
489186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
489286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
48931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
489486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
489586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
489686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
489786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
489886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
489986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
490086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
490186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (ArgTypes.size() == 0 && TypeStr[0] == '.')
490286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return getFunctionNoProtoType(ResType);
4903ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
4904ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor  // FIXME: Should we create noreturn types?
490586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4906ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                         TypeStr[0] == '.', 0, false, false, 0, 0,
4907264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                         FunctionType::ExtInfo());
490886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
4909a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4910a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType
4911a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4912a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Perform the usual unary conversions. We do this early so that
4913a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // integral promotions to "int" can allow us to exit early, in the
4914a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // lhs == rhs check. Also, for conversion purposes, we ignore any
4915a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // qualifiers.  For example, "const float" and "float" are
4916a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // equivalent.
4917a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isPromotableIntegerType())
4918a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = getPromotedIntegerType(lhs);
4919a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4920a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = lhs.getUnqualifiedType();
4921a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (rhs->isPromotableIntegerType())
4922a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = getPromotedIntegerType(rhs);
4923a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4924a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = rhs.getUnqualifiedType();
4925a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4926a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If both types are identical, no conversion is needed.
4927a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs == rhs)
4928a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
49291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4930a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4931a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The caller can deal with this (e.g. pointer + int).
4932a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4933a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
49341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // At this point, we have two different arithmetic types.
49361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4937a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Handle complex types first (C99 6.3.1.8p1).
4938a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexType() || rhs->isComplexType()) {
4939a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the complex type.
49401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
4941a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
4942a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4943a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
49441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
4945a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
4946a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4947a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4948a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // This handles complex/complex, complex/float, or float/complex.
49491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // When both operands are complex, the shorter operand is converted to the
49501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // type of the longer, and that is the type of the result. This corresponds
49511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // to what is done when combining two real floating-point operands.
49521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // The fun begins when size promotion occur across type domains.
4953a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // From H&S 6.3.4: When one operand is complex and the other is a real
49541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // floating-point type, the less precise type is converted, within it's
4955a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // real or complex domain, to the precision of the other type. For example,
49561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // when combining a "long double" with a "double _Complex", the
4957a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // "double _Complex" is promoted to "long double _Complex".
4958a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
49591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (result > 0) { // The left side is bigger, convert rhs.
4961a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
49621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (result < 0) { // The right side is bigger, convert lhs.
4963a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
49641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
4965a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // At this point, lhs and rhs have the same rank/size. Now, make sure the
4966a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // domains match. This is a requirement for our implementation, C99
4967a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // does not require this promotion.
4968a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4969a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4970a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return rhs;
4971a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      } else { // handle "_Complex double, double".
4972a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs;
4973a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      }
4974a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4975a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs; // The domain/size match exactly.
4976a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4977a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Now handle "real" floating types (i.e. float, double, long double).
4978a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4979a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the real floating type.
4980a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isIntegerType()) {
4981a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the lhs floating point type.
4982a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4983a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4984a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isComplexIntegerType()) {
4985a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the complex floating point type.
4986a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(lhs);
4987a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4988a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isIntegerType()) {
4989a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the rhs floating point type.
4990a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4991a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
49921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isComplexIntegerType()) {
4993a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the complex floating point type.
4994a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(rhs);
4995a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4996a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // We have two real floating types, float/complex combos were handled above.
4997a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Convert the smaller operand to the bigger result.
4998a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
4999a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (result > 0) // convert the rhs
5000a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5001a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    assert(result < 0 && "illegal float comparison");
5002a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return rhs;   // convert the lhs
5003a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5004a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5005a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Handle GCC complex int extension.
5006a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5007a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5008a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
5009a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhsComplexInt && rhsComplexInt) {
50101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
5011a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman                              rhsComplexInt->getElementType()) >= 0)
5012a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs; // convert the rhs
5013a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5014a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (lhsComplexInt && rhs->isIntegerType()) {
5015a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
5016a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5017a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (rhsComplexInt && lhs->isIntegerType()) {
5018a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
5019a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5020a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5021a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5022a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Finally, we have two differing integer types.
5023a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The rules for this case are in C99 6.3.1.8
5024a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  int compare = getIntegerTypeOrder(lhs, rhs);
5025a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  bool lhsSigned = lhs->isSignedIntegerType(),
5026a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman       rhsSigned = rhs->isSignedIntegerType();
5027a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  QualType destType;
5028a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhsSigned == rhsSigned) {
5029a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Same signedness; use the higher-ranked type
5030a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = compare >= 0 ? lhs : rhs;
5031a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (compare != (lhsSigned ? 1 : -1)) {
5032a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The unsigned type has greater than or equal rank to the
5033a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // signed type, so use the unsigned type
5034a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? rhs : lhs;
5035a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5036a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The two types are different widths; if we are here, that
5037a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // means the signed type is larger than the unsigned type, so
5038a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // use the signed type.
5039a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? lhs : rhs;
5040a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else {
5041a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The signed type is higher-ranked than the unsigned type,
5042a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // but isn't actually any bigger (like unsigned int and long
5043a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // on most 32-bit systems).  Use the unsigned type corresponding
5044a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // to the signed type.
5045a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5046a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5047a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return destType;
5048a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
5049