ASTContext.cpp revision ceafbdeb93ecf323cca74e660bf54504c86f3b71
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
15bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck#include "clang/AST/CharUnits.h"
1649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
17980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
18aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
21ea1471e0e967548c596a71469702f8846dbaf3c0John McCall#include "clang/AST/ExprCXX.h"
222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2319cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
241b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
25a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
27f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
2885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
296fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
30f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
3129445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson#include "RecordLayoutBuilder.h"
3229445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3961710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
40444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel Dunbar                       const TargetInfo &t,
41e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
421b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       bool FreeMem, unsigned size_reserve) :
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
452bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  NSConstantStringTypeDecl(0),
46782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
47083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
48c6fbbedb3e90ff2f04828c36fd839e01468679f5Douglas Gregor  SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
50ac9590effa90406767a544005ed1de52e258306bTed Kremenek  BuiltinInfo(builtins),
51ac9590effa90406767a544005ed1de52e258306bTed Kremenek  DeclarationNames(*this),
52ac9590effa90406767a544005ed1de52e258306bTed Kremenek  ExternalSource(0), PrintingPolicy(LOpts),
530c01d18094100db92d38daa923c95661512db203John McCall  LastSDM(0, 0) {
540f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCIdRedefinitionType = QualType();
550f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCClassRedefinitionType = QualType();
56369a3bd9979cf529eed529aa037de713c213e47dFariborz Jahanian  ObjCSelRedefinitionType = QualType();
571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
58e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
5914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
60e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
61e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
633478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
643478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
653478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
670054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  if (!FreeMemory) {
680054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor    // Call all of the deallocation functions.
690054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor    for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
700054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor      Deallocations[I].first(Deallocations[I].second);
710054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  }
720054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
737d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  // Release all of the memory associated with overridden C++ methods.
747d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
757d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor         OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
767d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor       OM != OMEnd; ++OM)
777d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    OM->second.Destroy();
783478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
79bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek  if (FreeMemory) {
80bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    // Deallocate all the types.
81bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    while (!Types.empty()) {
82bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      Types.back()->Destroy(*this);
83bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      Types.pop_back();
84bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    }
85b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
86bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    for (llvm::FoldingSet<ExtQuals>::iterator
87bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek         I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
88bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek      // Increment in loop to prevent using deallocated memory.
890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Deallocate(&*I++);
90bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    }
910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
92503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
93503524acc73f2f8280080ca0d200377406001b65Ted Kremenek         I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
94503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      // Increment in loop to prevent using deallocated memory.
95503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
96503524acc73f2f8280080ca0d200377406001b65Ted Kremenek        R->Destroy(*this);
97503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    }
98b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
99503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    for (llvm::DenseMap<const ObjCContainerDecl*,
100503524acc73f2f8280080ca0d200377406001b65Ted Kremenek         const ASTRecordLayout*>::iterator
101503524acc73f2f8280080ca0d200377406001b65Ted Kremenek         I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
102503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      // Increment in loop to prevent using deallocated memory.
103503524acc73f2f8280080ca0d200377406001b65Ted Kremenek      if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
104503524acc73f2f8280080ca0d200377406001b65Ted Kremenek        R->Destroy(*this);
105503524acc73f2f8280080ca0d200377406001b65Ted Kremenek    }
106b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
107b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
108ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
1091ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
1101ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
1111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         NNSEnd = NestedNameSpecifiers.end();
112bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek       NNS != NNSEnd; ) {
113bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek    // Increment in loop to prevent using deallocated memory.
1141ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
115bbfd68dd5288c435cfd6aef1264263e5f856958dTed Kremenek  }
116ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
117ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
118ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
119ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
120b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1230054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
1240054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
1250054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
1260054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
1302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
1357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
136dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
138dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
139dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
140dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
141dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
142c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
145dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
148dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
149dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
150dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
151dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
152dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
153dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
154dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
155dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
156dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
158dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
167e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
1686b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
169e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
1706b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
18215b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
20464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
2052df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
2062df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
2072df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
2082df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
2093a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
2103a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
2113a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
2123a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
21364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
214f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
215f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
216f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
217f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
218f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
219f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
220f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
221f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
222f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
223f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2248e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
225898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
226898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
227898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
228898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
229898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
230898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
231898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
232898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
2338e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Placeholder type for C++0x auto declarations whose real type has
235e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // not yet been deduced.
236e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
2428e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2437e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
245de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
246de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
247de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
24813dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = QualType();
2491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25013dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
251de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
252de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
25313dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
25414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
255a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
2561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
25833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2596e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2606e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2616e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
264251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
265663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
2667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
267663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
2687caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
2697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
2707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2727caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
2737caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2747caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
276251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
277251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor                                                TemplateSpecializationKind TSK) {
2787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
2797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
2807caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
2817caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
282251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
283251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor    = new (*this) MemberSpecializationInfo(Tmpl, TSK);
2847caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2857caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
287ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
2887ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
289ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
290ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
2910d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2930d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
2940d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
2950d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
2960d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
297ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
298ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
299ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
300ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
301ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
302ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
303ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
304ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
305ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
306ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
307ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
308ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
309ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
310ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
311ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
312ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
313ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
314ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
315ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
316ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
317ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
318ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
319ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
320ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
3210d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
3220d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
323d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
324d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
325d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
326d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
327d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
329d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
330d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
331d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
332d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
333d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
334d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
335d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
336d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
337d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
339d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
340d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
341d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
3427d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
3437d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
3447d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
3457d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
3467d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
3477d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
3487d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3497d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
3507d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
3537d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
3547d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
3557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
3567d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
3577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
3587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
3607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
3637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
3647d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
3657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
3667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace {
3681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  class BeforeInTranslationUnit
3692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    : std::binary_function<SourceRange, SourceRange, bool> {
3702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    SourceManager *SourceMgr;
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  public:
3732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    bool operator()(SourceRange X, SourceRange Y) {
3762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
3772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
3782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  };
3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
3802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
381464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
382464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
383464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
384a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
385b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
386b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
387b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
388183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
389b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
390b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
391b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
392b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
393b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
394b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
395b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
396b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
397b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
3988b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
399af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
400af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
4015d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
4025d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
4038b752f10c394b140f9ef89e049cbad1a7676fc25Ken DyckCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
404dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
405dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
40640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
407bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    Align = std::max(Align, AA->getMaxAlignment());
408dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
409af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
410af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
4116217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
4125d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
4135d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
4145d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
4155d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
4165d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
4175d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
4184cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
419dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
420dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
421dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
422dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
423dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
42405f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
42505f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      // In the case of a field in a packed struct, we want the minimum
42605f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      // of the alignment of the field and the alignment of the struct.
42705f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis      Align = std::min(Align,
42805f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis        getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
42905f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
430af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
431dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
4328b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck  return CharUnits::fromQuantity(Align / Target.getCharWidth());
433af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
434b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
435ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
436ea1471e0e967548c596a71469702f8846dbaf3c0John McCallASTContext::getTypeInfoInChars(const Type *T) {
437ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
438ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
439ea1471e0e967548c596a71469702f8846dbaf3c0John McCall                        CharUnits::fromQuantity(Info.second / getCharWidth()));
440ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
441ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
442ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
443ea1471e0e967548c596a71469702f8846dbaf3c0John McCallASTContext::getTypeInfoInChars(QualType T) {
444ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
445ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
446ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
447a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
448a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
4490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
4500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
4510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
4520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
453d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
4541d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
4555e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
4565e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
457a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
45872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
45972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
46018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
46172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
46272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
46318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
46472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
46572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
4665d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
4675d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
46818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
46918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
47018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
47118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
47218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
47372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
474fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
47518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
47618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
47718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
47818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
479fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
4801d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48298be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
4839e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
484030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
485030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
4865c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
487213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
488030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
4899fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
4909fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
4919fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
4924bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
4936fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
4946fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
4958eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
4969fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
4979fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
4989fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
499030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
500030d8846c7e520330007087e949f621989876e3aChris Lattner  }
5015d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
5029e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
503a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
504692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
505d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
50618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
50718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
50818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
50918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
51018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5116f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
5129e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
5139e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
5146f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
515692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
516692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
517692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
5186f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
5199e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
5209e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
5216f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
52264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
52364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
52464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
52564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
526f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
527f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
528f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
529f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
530f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
531f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
532f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
533f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
534692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
5356f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
5369e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
5379e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
5386f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
539692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
5406f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
5419e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
5429e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
5436f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
544692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
5456f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
5469e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
5479e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
5486f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
549692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
5506f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
5519e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
5529e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
5536f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
554ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
555ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
556ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
557ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
558ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
5596f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
5609e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
5619e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
5626f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5636f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
5645426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
5655426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
5666f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5676f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
5689e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
5699e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
5706f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5716e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
5726e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
5736e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
5741590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
575a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
576bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
577d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
5785426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
579f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
5806f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
581485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
582485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
583485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
584485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
585485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
586485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
5875d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
5885d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
5895d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
5905d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
5915d484e8cf710207010720589d89602233de61d01Sebastian Redl    unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
5925d484e8cf710207010720589d89602233de61d01Sebastian Redl    Width = Target.getPointerWidth(AS);
5935d484e8cf710207010720589d89602233de61d01Sebastian Redl    Align = Target.getPointerAlign(AS);
5945d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
5955d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
596f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
597f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
5985426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
599f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
600f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
601f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
602f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
603f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
6051cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
6061cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
607f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
608f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
6091cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
6101cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
611f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
6125d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
6135d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
6145d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
61698be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
6179e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
6185d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
6195d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
6205d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
621c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
622c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
62344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
6241d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
62544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
62644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
62744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
62844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
62944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
63072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
63172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
6321d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
6331d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
6341d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
6358389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
6368389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
6378389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
6388389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6401d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
6417176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
6427176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
6431d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
6447176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
6457176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
6467176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
647dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
648a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
6497532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
6509fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
65149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
65249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
65349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
65418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
65518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
65640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
657bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      Align = std::max(Aligned->getMaxAlignment(),
658bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt                       getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
65918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
66018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
66118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
6627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
6637176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
66418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
66518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
66618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
66718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
66818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
66918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
67018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
67118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
672395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
673395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
674395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
675395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
676465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
677465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
68118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
68218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
68318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
68418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
68518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
68618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
688464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
6899e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
690a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
691a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
692bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
693bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
694bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen DyckCharUnits ASTContext::getTypeSizeInChars(QualType T) {
695199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
696bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
697bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen DyckCharUnits ASTContext::getTypeSizeInChars(const Type *T) {
698199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
699bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
700bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
70116e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
70286fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
70386fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken DyckCharUnits ASTContext::getTypeAlignInChars(QualType T) {
70486fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck  return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
70586fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
70686fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken DyckCharUnits ASTContext::getTypeAlignInChars(const Type *T) {
70786fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck  return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
70886fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
70986fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
71034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
71134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
71234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
71334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
71434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
71534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
7161eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
7171eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
718183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
7191eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
7201eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
7211eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
7221eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
7231eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
72434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
72534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
72634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
727a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
728a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
729a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
730a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
731a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
732f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
733a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
734a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
735a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
736a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
737a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
738a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
739a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
740a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
741a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
742a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
743a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
744a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
7458e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
7468e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
7478e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
7488e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
74911062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
7508e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
7518e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian         E = OI->ivar_end(); I != E; ++I) {
7528e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian     Ivars.push_back(*I);
7538e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
75411062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian
75511062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  CollectNonClassIvars(OI, Ivars);
7568e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
7578e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
75811062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian/// CollectNonClassIvars -
75911062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian/// This routine collects all other ivars which are not declared in the class.
7607d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek/// This includes synthesized ivars (via @synthesize) and those in
7617d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek//  class's @implementation.
7629820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
76311062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanianvoid ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
7649820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
7650e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  // Find ivars declared in class extension.
7660e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
7670e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian    for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
7680e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian         E = CDecl->ivar_end(); I != E; ++I) {
7690e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian      Ivars.push_back(*I);
7700e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian    }
7710e5ad255729ee86b8ed57e659029008984517cdeFariborz Jahanian  }
77211062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian
7737d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek  // Also add any ivar defined in this class's implementation.  This
7747d2aa11c5c3b85a9d727ff868de666ba3bff5b59Ted Kremenek  // includes synthesized ivars.
77511062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
77611062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian    for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
77711062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian         E = ImplDecl->ivar_end(); I != E; ++I)
77811062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian      Ivars.push_back(*I);
77911062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  }
7809820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
7819820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
782e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
783e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
784e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
785432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
786e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
787e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
788e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OI->protocol_end(); P != PE; ++P) {
789e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
790432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
791e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
792b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
793b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian        Protocols.insert(*P);
794e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
795e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
796b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
797e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
798e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
799e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
800e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
801e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
802e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
803e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
804e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
805e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
806e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
807b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
808e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
809e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
810e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
811432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
812e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
813e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
814e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
815e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
816b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
817e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
818e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
819e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
820432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
821e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
822e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
823e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
824e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
825e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
826e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
827e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
8283bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanianunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
8293bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
8303bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
831b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  if (const ObjCCategoryDecl *CDecl = OI->getClassExtension())
832b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
833b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
8343bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
8353bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
8363bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
837b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
838b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
8398e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8408e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8418e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8428a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
8438a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
8448a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8458a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8468a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8478a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
8488a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8498a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8508a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
8518a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
8528a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8538a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
8568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
8598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
8608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
8618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
8628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
8638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
8648a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8658a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
8668a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
8678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
8688a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
8698a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
8708a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8718a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
872a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Allocate an uninitialized TypeSourceInfo.
873b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
874a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// The caller should initialize the memory held by TypeSourceInfo using
875b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
876b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
877b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
878b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
879b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
880a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
881109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall                                                 unsigned DataSize) {
882109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
883109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
884109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
885109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
886a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
887109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
888a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
889a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
890a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
891a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
892b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
893b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
894a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
895a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall                                                     SourceLocation L) {
896a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
897a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  DI->getTypeLoc().initialize(L);
898a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
899a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
900a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
901b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the
902b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface.
903b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar///
904b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's
905b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars.
90644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout &
907b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
908b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar                          const ObjCImplementationDecl *Impl) {
909532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar  assert(!D->isForwardDecl() && "Invalid interface decl!");
910532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar
91144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Look up this layout, if already laid out, return what we have.
9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ObjCContainerDecl *Key =
913d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
914d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
915d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    return *Entry;
91644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
917453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // Add in synthesized ivar count if laying out an implementation.
918453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
9193bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian    unsigned SynthCount = CountNonClassIvars(D);
920d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    // If there aren't any sythesized ivars then reuse the interface
921453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entry. Note we can't cache this because we simply free all
922453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entries later; however we shouldn't look up implementations
923453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // frequently.
9248e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if (SynthCount == 0)
925453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar      return getObjCLayout(D, 0);
926453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  }
927453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
92929445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
93029445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  ObjCLayouts[Key] = NewEntry;
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  return *NewEntry;
93344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel}
93444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
935b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
936b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
937b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
938b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
939b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
940b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
941b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
942b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
943b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
944b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
94588a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
946464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
947464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
94898be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
949952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  D = D->getDefinition();
9504b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  assert(D && "Cannot get layout of forward declarations!");
9514bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
952464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
953ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // Note that we can't save a reference to the entry because this function
954ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // is recursive.
955ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  const ASTRecordLayout *Entry = ASTRecordLayouts[D];
956464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
9574bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
95929445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D);
960ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  ASTRecordLayouts[D] = NewEntry;
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9628d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar  if (getLangOptions().DumpRecordLayouts) {
9638d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar    llvm::errs() << "\n*** Dumping AST Record Layout\n";
9648d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar    DumpRecordLayout(D, llvm::errs());
9658d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar  }
9668d8ab749f6f8bb63ea2cd2b589c0f050b67fc5ccDaniel Dunbar
9675d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
968464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
969464175bba1318bef7905122e9fda20cff926df78Chris Lattner
970f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlssonconst CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
971952b017601f9c82b51119c3a1600f1312a833db9Douglas Gregor  RD = cast<CXXRecordDecl>(RD->getDefinition());
972f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  assert(RD && "Cannot get key function for forward declarations!");
973f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson
974f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  const CXXMethodDecl *&Entry = KeyFunctions[RD];
975f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  if (!Entry)
976f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson    Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
977f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  else
978f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson    assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
979f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson           "Key function changed!");
980f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson
981f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson  return Entry;
982f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson}
983f53df2398e07d13be9962b95aebc19b31706fa33Anders Carlsson
984a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
985a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
986a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
987a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
9880953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
9890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  unsigned Fast = Quals.getFastQualifiers();
9900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeFastQualifiers();
9910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
9920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
9930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
9940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQuals::Profile(ID, TypeNode, Quals);
9950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  void *InsertPos = 0;
9960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
9970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert(EQ->getQualifiers() == Quals);
9980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType T = QualType(EQ, Fast);
9990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return T;
10000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
10010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10026b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
10030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQualNodes.InsertNode(New, InsertPos);
10040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType T = QualType(New, Fast);
10050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return T;
10060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
10070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10080953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getVolatileType(QualType T) {
10090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanT = getCanonicalType(T);
10100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (CanT.isVolatileQualified()) return T;
10110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addVolatile();
10150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
10170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
10180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1019f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
1020f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1021f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1022f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1023b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
10240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
10250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
10260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
10300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
10310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
10320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
10330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
10341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1036ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1037ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1038b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
10390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                       Qualifiers::GC GCAttr) {
1040d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1041b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1042d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10444027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  if (T->isPointerType()) {
10456217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
104658f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
10474027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
10484027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
10494027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
10504027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
10511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
10530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
10540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
10580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
10590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
10600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
10610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
10621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1064d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1065a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1066264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindolastatic QualType getExtFunctionType(ASTContext& Context, QualType T,
1067264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                        const FunctionType::ExtInfo &Info) {
10680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType ResultType;
106943c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  if (const PointerType *Pointer = T->getAs<PointerType>()) {
107043c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    QualType Pointee = Pointer->getPointeeType();
1071264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    ResultType = getExtFunctionType(Context, Pointee, Info);
107243c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (ResultType == Pointee)
107343c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1074ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1075ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    ResultType = Context.getPointerType(ResultType);
107643c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  } else if (const BlockPointerType *BlockPointer
107743c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor                                              = T->getAs<BlockPointerType>()) {
107843c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    QualType Pointee = BlockPointer->getPointeeType();
1079264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    ResultType = getExtFunctionType(Context, Pointee, Info);
108043c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (ResultType == Pointee)
108143c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1082ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1083ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    ResultType = Context.getBlockPointerType(ResultType);
1084ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor   } else if (const FunctionType *F = T->getAs<FunctionType>()) {
1085264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    if (F->getExtInfo() == Info)
108643c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      return T;
1087ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
108843c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
1089ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor      ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
1090264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                                  Info);
10910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    } else {
109243c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor      const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
10930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      ResultType
1094ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor        = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1095ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getNumArgs(), FPT->isVariadic(),
1096ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getTypeQuals(),
1097ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->hasExceptionSpec(),
1098ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->hasAnyExceptionSpec(),
1099ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->getNumExceptions(),
1100ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor                                  FPT->exception_begin(),
1101264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                  Info);
11020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
110343c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor  } else
110443c79c2b07abc7ba6d9f243b84ee6539de4d2652Douglas Gregor    return T;
1105ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1106ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1107ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor}
1108ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1109ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas GregorQualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
1110425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1111264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getExtFunctionType(*this, T,
1112264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                 Info.withNoReturn(AddNoReturn));
1113ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor}
1114ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
1115ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas GregorQualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
1116425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1117264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getExtFunctionType(*this, T,
1118264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                            Info.withCallingConv(CallConv));
11192455636163fdd18581d7fdae816433f886d88213Mike Stump}
11202455636163fdd18581d7fdae816433f886d88213Mike Stump
1121425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael EspindolaQualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1122425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1123425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  return getExtFunctionType(*this, T,
1124425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                                 Info.withRegParm(RegParm));
1125425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola}
1126425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
11381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1142467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1143f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1147f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11496b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
11505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
11621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1170467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1171f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1175f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11776b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
11845618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
11855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
1186296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1187296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
11885618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
11895618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
11905618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11925618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
11935618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
11945618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11955618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
11961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
11985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
11995618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1200467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
12015618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
12045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
12055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1206f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12075618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
12086b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
12096b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
12105618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
12115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
12125618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
12135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
12145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
12157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
12167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
121754e14c4db764c0636160d26c5bbf491637c83a76John McCallQualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
122154e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
12247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
12257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
12277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
122854e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
122954e14c4db764c0636160d26c5bbf491637c83a76John McCall
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
123354e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
123454e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
123554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
12367c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
12387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
12397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
12407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
12427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12436b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
124454e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
124554e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
12467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
12477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
124854e14c4db764c0636160d26c5bbf491637c83a76John McCall
12497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
12507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
12517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
12537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
12547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
12557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
12567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
12577c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
125854e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
12597c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
12617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
12627c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
12647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
126554e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
126654e14c4db764c0636160d26c5bbf491637c83a76John McCall
12677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
12687c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
12697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
127054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
127154e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
127254e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
12737c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
12767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1277f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12806b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
12816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1287f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1288f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
12891eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
1290f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1291f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1292f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1293f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1294f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1295f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1296f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1297f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1298f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1299f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1300f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1301f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1302f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
130387c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1304f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1305f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1306f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1307f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1308f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1309f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1310f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
13116b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
13126b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1313f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1314f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1315f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1316f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1317f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1319fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
13201eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
132138aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1322c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1323c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1324923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1325923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1326587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1327587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
132838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
132938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
133038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
133138aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
13340be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
13351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
13387192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1344467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!EltTy.isCanonical()) {
13451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1346c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
13481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
13497192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1350f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13536b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
13546b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
13557192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
13565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1360bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1361bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
13627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
13637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1364c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
13657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          unsigned EltTypeQuals,
13667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          SourceRange Brackets) {
1367c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1368c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1369715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  QualType CanonType;
1370715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
1371715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  if (!EltTy.isCanonical()) {
1372715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor    if (NumElts)
1373715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor      NumElts->Retain();
1374715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor    CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1375715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor                                     EltTypeQuals, Brackets);
1376715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
1377715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
13786b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
1379715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor    VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
1380c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1381c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1382c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1383c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1384c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1385c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1386898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1387898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
138804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
13897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy,
13907e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                Expr *NumElts,
1391898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
13927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                unsigned EltTypeQuals,
13937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                SourceRange Brackets) {
1394cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  assert((!NumElts || NumElts->isTypeDependent() ||
1395cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor          NumElts->isValueDependent()) &&
1396898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1397898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
139804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  void *InsertPos = 0;
1399cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  DependentSizedArrayType *Canon = 0;
1400789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
1401cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1402cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  if (NumElts) {
1403cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // Dependently-sized array types that do not have a specified
1404cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // number of elements will have their sizes deduced from an
1405cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    // initializer.
1406cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1407cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor                                     EltTypeQuals, NumElts);
1408cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1409cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor    Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1410cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
1411cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
141204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *New;
141304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  if (Canon) {
141404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // We already have a canonical version of this array type; use it as
141504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // the canonical type for a newly-built type.
14166b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
14176b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
14186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                              NumElts, ASM, EltTypeQuals, Brackets);
141904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  } else {
142004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    QualType CanonEltTy = getCanonicalType(EltTy);
142104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    if (CanonEltTy == EltTy) {
14226b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
14236b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedArrayType(*this, EltTy, QualType(),
14246b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                NumElts, ASM, EltTypeQuals, Brackets);
1425cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
1426789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      if (NumElts) {
1427789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        DependentSizedArrayType *CanonCheck
1428789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor          = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1429789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        assert(!CanonCheck && "Dependent-sized canonical array type broken");
1430789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        (void)CanonCheck;
1431cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor        DependentSizedArrayTypes.InsertNode(New, InsertPos);
1432789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      }
143304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    } else {
143404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
143504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  ASM, EltTypeQuals,
143604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  SourceRange());
14376b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
14386b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedArrayType(*this, EltTy, Canon,
14396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                NumElts, ASM, EltTypeQuals, Brackets);
144004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    }
144104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  }
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1443898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1444898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1445898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1446898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1447c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1448c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1449c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1450c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
14510be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1452c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1453c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
14541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (IncompleteArrayType *ATP =
1455c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1456c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1457c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1458c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1459c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1460c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1461c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1462467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!EltTy.isCanonical()) {
1463f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
14642bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1465c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1466c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1467c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1468c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1469f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14702bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1471c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
14726b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  IncompleteArrayType *New = new (*this, TypeAlignment)
14736b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
1474c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1475c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1476c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1477c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1478fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1479fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
148073322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
148173322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
148282287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
148382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson                                   bool IsAltiVec, bool IsPixel) {
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1486f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
148773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
14881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
149182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  VectorType::Profile(ID, vecType, NumElts, Type::Vector,
149282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    IsAltiVec, IsPixel);
14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
150082287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
150182287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Canonical = getVectorType(getCanonicalType(vecType),
150282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson      NumElts, false, false);
15031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1506f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15086b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
150982287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1515213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
151673322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1517213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
151873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1520f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1521213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
152473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
152582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
152673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
152773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
152873322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
152973322924127c873c13101b705dd823f5539ffa5fSteve Naroff
153073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
153173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
153273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
1533467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
1534213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
153673322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
153773322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1538f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
153973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
15406b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
15416b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
154273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
154373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
154473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
154573322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
154673322924127c873c13101b705dd823f5539ffa5fSteve Naroff
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
15489cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    Expr *SizeExpr,
15499cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    SourceLocation AttrLoc) {
15502ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
15511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
15522ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15542ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
15552ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
15562ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
15572ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
15582ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
15592ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
15602ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
15616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
15626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
15636b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
15642ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
15652ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
15662ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
15676b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
15686b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
15696b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
1570789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1571789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
1572789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1573789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1574789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
15752ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
15762ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
15772ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
15782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
15796b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
15806b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
15812ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
15822ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
15859cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
15869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
158872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
1590264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael EspindolaQualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1591264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                            const FunctionType::ExtInfo &Info) {
1592264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  const CallingConv CallConv = Info.getCC();
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1596264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
15971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
160072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
16021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1604ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
160504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
1606264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
1607264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
1608264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
161172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
161272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1613f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16166b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
1617264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    FunctionNoProtoType(ResultTy, Canonical, Info);
16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
161972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
162561710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1626971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1627465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     unsigned TypeQuals, bool hasExceptionSpec,
1628465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     bool hasAnyExceptionSpec, unsigned NumExs,
1629264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                     const QualType *ExArray,
1630264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                     const FunctionType::ExtInfo &Info) {
1631264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  const CallingConv CallConv= Info.getCC();
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
163572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1636465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                             TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1637264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                             NumExs, ExArray, Info);
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
164172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1643465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1644465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
164554e14c4db764c0636160d26c5bbf491637c83a76John McCall  bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
164754e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
16495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
1651465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
165304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
16545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
16555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
165754e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
1658465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1659f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1660beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
166147259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                isVariadic, TypeQuals, false,
1662264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                false, 0, 0,
1663264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
1664465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
16655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
166672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
166772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1668f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1670465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
167172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1672465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
1673465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // end of them.
16741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionProtoType *FTP =
1675465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1676465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumArgs*sizeof(QualType) +
16776b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                 NumExs*sizeof(QualType), TypeAlignment);
167872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1679465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                              TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1680264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                              ExArray, NumExs, Canonical, Info);
16815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
168272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
16835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
16845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
16873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
16883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
16893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
16903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
16913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
16923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
16933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
16943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
16953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
16963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
16973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
16983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
16993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
17003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
17013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
17023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              QualType TST) {
17033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
17043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
17053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
17063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else if (CXXRecordDecl *PrevDecl
17073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall               = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
17083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
17093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
17103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
17113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
171231f17ecbef57b5679c017c375db330546b7b5145John McCall    Decl->TypeForDecl =
171331f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
17143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Types.push_back(Decl->TypeForDecl);
17153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
17163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
17173cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
17183cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
17192ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
17202ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
1721becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCallQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
17221e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
1723becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
17241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
172519c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
17262ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1727becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
1728becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
1729becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
1730becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
173119c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1732becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Record->getPreviousDeclaration() &&
1733becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
1734becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
1735becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
173619c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1737becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Enum->getPreviousDeclaration() &&
1738becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
1739becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
174019c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
1741ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1742ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
17439fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
1744becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
174549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1746becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  Types.push_back(Decl->TypeForDecl);
174749aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
17482ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
17492ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
17505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
17515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
175219c8576b7328f4dc2d07682f5da552875c1912efJohn McCallQualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
17535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1755f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
17566b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Decl->TypeForDecl = new(*this, TypeAlignment)
17576b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
17585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
17595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
17605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
176249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
176349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
176449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
176549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                                         QualType Replacement) {
1766467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
176749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
176849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
176949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
177049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
177149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
177249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
177349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
177449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
177549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
177649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
177749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
177849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
177949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
178049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
178149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
178249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
178349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
178449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
1785fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
17861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
178776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
17881eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
178976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
1790fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1791fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
179276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
1793fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
17941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
1795fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1796fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1797fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1798fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
17991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
180076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  if (Name) {
180176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
18026b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
18036b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
1804789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1805789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
1806789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1807789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
1808789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
180976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
18106b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
18116b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
1812fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1813fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1814fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1815fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1816fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1817fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1818fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
18193cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
18203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
18213cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
18223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
18233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              QualType CanonType) {
18243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
18253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
18263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
18273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
18283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
18293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
18303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
18313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
18323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
18333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
18343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
18353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
18363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
18371eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
18387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
1839d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
184031f17ecbef57b5679c017c375db330546b7b5145John McCall                                          QualType Canon,
184131f17ecbef57b5679c017c375db330546b7b5145John McCall                                          bool IsCurrentInstantiation) {
1842d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
1843d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1844833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  llvm::SmallVector<TemplateArgument, 4> ArgVec;
1845833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
1846833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
1847833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
1848833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
184931f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
185031f17ecbef57b5679c017c375db330546b7b5145John McCall                                       Canon, IsCurrentInstantiation);
1851833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
1852833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1853833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
1854833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
18557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
18567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
185731f17ecbef57b5679c017c375db330546b7b5145John McCall                                          QualType Canon,
185831f17ecbef57b5679c017c375db330546b7b5145John McCall                                          bool IsCurrentInstantiation) {
1859b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  if (!Canon.isNull())
1860b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    Canon = getCanonicalType(Canon);
1861b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  else {
186231f17ecbef57b5679c017c375db330546b7b5145John McCall    assert(!IsCurrentInstantiation &&
186331f17ecbef57b5679c017c375db330546b7b5145John McCall           "current-instantiation specializations should always "
186431f17ecbef57b5679c017c375db330546b7b5145John McCall           "have a canonical type");
186531f17ecbef57b5679c017c375db330546b7b5145John McCall
1866b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    // Build the canonical template specialization type.
18671275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateName CanonTemplate = getCanonicalTemplateName(Template);
18681275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::SmallVector<TemplateArgument, 4> CanonArgs;
18691275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    CanonArgs.reserve(NumArgs);
18701275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
18711275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
18721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
18731275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // Determine whether this canonical template specialization type already
18741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // exists.
18751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::FoldingSetNodeID ID;
187631f17ecbef57b5679c017c375db330546b7b5145John McCall    TemplateSpecializationType::Profile(ID, CanonTemplate, false,
1877828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                        CanonArgs.data(), NumArgs, *this);
18781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
18791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    void *InsertPos = 0;
18801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateSpecializationType *Spec
18811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
18821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    if (!Spec) {
18841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // Allocate a new canonical template specialization type.
18851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      void *Mem = Allocate((sizeof(TemplateSpecializationType) +
18861275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                            sizeof(TemplateArgument) * NumArgs),
18876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                           TypeAlignment);
188831f17ecbef57b5679c017c375db330546b7b5145John McCall      Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
18891275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                                  CanonArgs.data(), NumArgs,
1890b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                                                  Canon);
18911275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Types.push_back(Spec);
18921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
18931275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
18941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1895b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    if (Canon.isNull())
1896b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor      Canon = QualType(Spec, 0);
18971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(Canon->isDependentType() &&
18981275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           "Non-dependent template-id type must have a canonical type");
1899b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  }
1900fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
19011275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
19021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
19031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
19041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
190540808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
19066b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
19071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
190831f17ecbef57b5679c017c375db330546b7b5145John McCall    = new (Mem) TemplateSpecializationType(*this, Template,
190931f17ecbef57b5679c017c375db330546b7b5145John McCall                                           IsCurrentInstantiation,
191031f17ecbef57b5679c017c375db330546b7b5145John McCall                                           Args, NumArgs,
1911828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                           Canon);
19121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
191355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
19141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
191555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
191655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
19171eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1918465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
1919465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
1920465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              QualType NamedType) {
1921e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1922465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
1923e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1924e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
1925465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1926e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1927e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1928e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1929789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
1930789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
1931789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
1932465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1933465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
1934789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
1935789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
1936789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1937465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
1938e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1939465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
1940e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1941e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1942e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
19434a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
19444a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
19454a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
19464a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          QualType Canon) {
1947d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1948d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1949d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1950d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
19514a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
19524a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
19534a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
19544a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
19554a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
19564a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
1957d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1958d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1959d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
19604a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
1961d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1962d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
19634714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
19644714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1965d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1966d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1967d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
19684a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
1969d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
19704714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
19711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
1972d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1973d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
19741eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
19754a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
19764a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
19774a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 const TemplateSpecializationType *TemplateId,
19784a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 QualType Canon) {
19791734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
19801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
1981789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
19824a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
1983789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1984789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
19854714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
19864714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1987789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
1988789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
1989789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
19901734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
19911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
19921734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
19934a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
19944a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
19954a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
19964a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword ||
19974a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor        CanonType != QualType(TemplateId, 0)) {
19981734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
1999183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = CanonType->getAs<TemplateSpecializationType>();
20001734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
20011734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
20024a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
20031734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
20041734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
20054714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    DependentNameType *CheckT
20064714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor      = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2007789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
2008789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
20091734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
20104a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
20111734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
20124714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
20131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
20141734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
20151734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
201688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
201788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
201888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
201988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
20202e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
202188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
202288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2023c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
202454e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
202554e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
202654e14c4db764c0636160d26c5bbf491637c83a76John McCall
202754e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
202854e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
202954e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
203054e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
203154e14c4db764c0636160d26c5bbf491637c83a76John McCall}
203254e14c4db764c0636160d26c5bbf491637c83a76John McCall
203354e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
203488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
203588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
203788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
203888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
203988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
204088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
204188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
204288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
204388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
204488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2045c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
2046c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
2047c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       unsigned NumProtocols) {
2048c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
2049c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
2050c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2051c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
2052d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2053c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
2054c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
2055c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
2056d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
2057c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2058c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
2059d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2060c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
2061c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
206254e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2063c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2064c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
2065c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
20660237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
20670237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
206854e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
206954e14c4db764c0636160d26c5bbf491637c83a76John McCall
207054e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2071c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2072c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
207354e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
2074c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2075c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
207654e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
207754e14c4db764c0636160d26c5bbf491637c83a76John McCall
207854e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
2079c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
208054e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
208154e14c4db764c0636160d26c5bbf491637c83a76John McCall
2082c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
2083c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2084fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2085c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
2086c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
20871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2088c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2089c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
2090c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2091d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
209288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2093c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2094c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
2095c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
20964b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2097c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
20981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20994b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2100c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
2101c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
21024b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
21031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2104c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
210554e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2106c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
2107c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
210854e14c4db764c0636160d26c5bbf491637c83a76John McCall
2109c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
2110c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
211154e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
211254e14c4db764c0636160d26c5bbf491637c83a76John McCall
2113c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
2114c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2115c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
2116c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
211724fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
211824fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
2119c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
212024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
212124fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
212224fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
2123c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCInterfaceType - Return the unique reference to the type for the
2124c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// specified ObjC interface decl. The list of protocols is optional.
2125c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2126c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (Decl->TypeForDecl)
2127c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(Decl->TypeForDecl, 0);
2128c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
2129c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // FIXME: redeclarations?
2130c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2131c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2132c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Decl->TypeForDecl = T;
2133c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2134c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2135c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
2136c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
213772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
213872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
21399752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
21401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
21419752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
214272564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
2143dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2144b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2145b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2146b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
21471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2148b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2149b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2150b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2151b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2152b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2153b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
21546b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
2155b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
2156b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2157b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
2158b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
21596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
21606b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
2161b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2162b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2163b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2164b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2165dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
21666b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
2167dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
21689752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
21699752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2170d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2171d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
21729752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
21739752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
21749752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
21751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
21769752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
2177d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
2178f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
21796b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
21809752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
21819752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2182d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2183d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
218460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
218560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
218660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
2187a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2188a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
21891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
219060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
219160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
219260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
219360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
219460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
219560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
219660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
219760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
219860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
219960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
220060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
220160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
220260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
220360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
220460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
22051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
220660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
220960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
221060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (e->isLvalue(Context) == Expr::LV_Valid)
221160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
22121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
221360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
221460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
221560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2216395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2217395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2218395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2220395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
2221395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) {
2222dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
22239d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  if (e->isTypeDependent()) {
22249d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
22259d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
22261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22279d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
22289d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
22299d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
22309d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
22319d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
22329d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
22336b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
22349d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
22359d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
22369d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
22379d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
22386b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
22399d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
22409d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
22419d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
22429d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2243dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
22446b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
2245dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2246395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2247395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2248395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2249395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
22505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
22515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
2252e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) {
2253d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2254e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2255e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2256e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
22575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
22601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
22611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
2262a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
2263b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
22645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
226664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
226764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
226864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
226964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
227064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
227164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
227264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
227364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
227464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
227564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
227664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
227764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
227864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
227964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
22808b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
22818b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
22828b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2283b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
22848b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
22858b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2286e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2287e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2288e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2289e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
229054e14c4db764c0636160d26c5bbf491637c83a76John McCallCanQualType ASTContext::getCanonicalParamType(QualType T) {
229154e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
229254e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
229354e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
229454e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
229554e14c4db764c0636160d26c5bbf491637c83a76John McCall
229654e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
229754e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
229854e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
229954e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
230054e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
230154e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
230254e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
230354e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
230454e14c4db764c0636160d26c5bbf491637c83a76John McCall
230554e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
230654e14c4db764c0636160d26c5bbf491637c83a76John McCall}
230754e14c4db764c0636160d26c5bbf491637c83a76John McCall
230877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
230977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
231077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
231177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
231277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
231377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
231450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) {
23150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
23160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ptr = Quals.strip(T);
23170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanType = Ptr->getCanonicalTypeInternal();
23180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
23190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // The canonical internal type will be the canonical type *except*
23200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // that we push type qualifiers down through array types.
23211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If there are no new qualifiers to push down, stop here.
23230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!Quals.hasQualifiers())
232450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType);
2325c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
23260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the type qualifiers are on an array type, get the canonical
23270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type of the array with the qualifiers applied to the element
23280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type.
2329c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
2330c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
23310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
23321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2333c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2334c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
23350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
2336c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
23371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2338c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
233950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
234050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getConstantArrayType(NewEltTy, CAT->getSize(),
234150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getSizeModifier(),
23420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                  CAT->getIndexTypeCVRQualifiers()));
2343c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
234450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
234550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
23460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    IAT->getIndexTypeCVRQualifiers()));
23471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2348898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
234950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
235050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getDependentSizedArrayType(NewEltTy,
2351bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                        DSAT->getSizeExpr() ?
2352bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                          DSAT->getSizeExpr()->Retain() : 0,
235350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getSizeModifier(),
23540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                        DSAT->getIndexTypeCVRQualifiers(),
235587a924e37dc40c6e3e2b33d1dbd646aeccf4c15bDouglas Gregor                        DSAT->getBracketsRange())->getCanonicalTypeInternal());
2356898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2357c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
235850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
2359bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                        VAT->getSizeExpr() ?
2360bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr()->Retain() : 0,
236150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                        VAT->getSizeModifier(),
23620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
236350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                     VAT->getBracketsRange()));
2364c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
2365c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
236628e318cc6008c2bc008f0caee70dc736a03d6289Chandler CarruthQualType ASTContext::getUnqualifiedArrayType(QualType T,
236728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                             Qualifiers &Quals) {
23685535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  Quals = T.getQualifiers();
23699dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const ArrayType *AT = getAsArrayType(T);
23709dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
23715535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth    return T.getUnqualifiedType();
237228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
237328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
237428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  QualType Elt = AT->getElementType();
2375c1ae0a8b0022a9fbb50d3413139f36ec5d8a63beZhongxing Xu  QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
237628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  if (Elt == UnqualElt)
237728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return T;
237828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
23799dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
238028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return getConstantArrayType(UnqualElt, CAT->getSize(),
238128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
238228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
238328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
23849dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
238528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth    return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
238628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
238728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
23889dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
23899dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor    return getVariableArrayType(UnqualElt,
23909dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeExpr() ?
23919dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeExpr()->Retain() : 0,
23929dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
23939dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
23949dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
23959dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
23969dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
23979dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
239828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
239928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
240028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
240128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
240228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
240380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCallDeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
240480ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (TemplateDecl *TD = Name.getAsTemplateDecl())
240580ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    return TD->getDeclName();
240680ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
240780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
240880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
240980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall      return DeclarationNames.getIdentifier(DTN->getIdentifier());
241080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
241180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall      return DeclarationNames.getCXXOperatorName(DTN->getOperator());
241280ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
241380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
241480ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
24150bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
24160bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(Storage);
24170bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return (*Storage->begin())->getDeclName();
241880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
241980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
242025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
242125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
242225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
242325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
242497fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
242525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
24260bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(!Name.getAsOverloadedTemplate());
24271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
242825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
242925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
243025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
243125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
243225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
2433db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2434db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
2435db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
2436db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
2437db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
2438db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
24391eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
24401275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
24411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
24421275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
24431275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
24441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
24461275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
24471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
2449833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
24501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2451788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
2452788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2453788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
24541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
2455833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(*Arg.getAsIntegral(),
24561275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
24571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24581275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
2459833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
24601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24611275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
24621275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Allocate in ASTContext
24631275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
24641275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
24651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
24661275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
24671275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
24681275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
24691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24701275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument Result;
24711275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
24721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Result;
24731275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
24741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
24751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
24761275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
24771275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
24781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
24791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
24801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
2481d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
2482d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
24831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
2484d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
2485d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2486d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
2487d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
2488d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
24891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
2490d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2491d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
2492d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2493d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
2494d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
2495d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
2496d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2497d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2498d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
2499d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
2500d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
25011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this, 0,
25021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2503d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
2504d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2505d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2506d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
2507d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
2508d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
2509d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2510d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2511d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
2512d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
2513d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2514d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2515c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2516c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
2517c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
2518a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
2519c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
2520c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2521c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
2522c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
25231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
2525c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
25260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!isa<ArrayType>(CType))
2527c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
25281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
2530c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
2531c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
25321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2533c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
2534c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
253550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
25360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
25370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
25380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ty = Qs.strip(T.getDesugaredType());
25391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2540c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
2541c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
25420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ATy == 0 || Qs.empty())
2543c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
25441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2545c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
2546c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
2547c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2548c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
25490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
25501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2551c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2552c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2553c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
25540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
2555c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2556c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2557c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
25580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
2559898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
25601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
2561898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
2562898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
25631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
2564bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                DSAT->getSizeExpr() ?
2565bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              DSAT->getSizeExpr()->Retain() : 0,
2566898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
25670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
25687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
25691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2570c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
25717e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
2572bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr() ?
25730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getSizeExpr()->Retain() : 0,
2574c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
25750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
25767e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
257777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
257877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
257977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
2580e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
2581e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
2582e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
2583e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
2584e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
2585e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2586e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
2587c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
2588c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
2589c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
2590c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
2591c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2592c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
25931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2594c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
2595e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2596e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
25970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
2598e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
2599e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
26005e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) {
26010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
26020237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer  while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
26030237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer    QT = AT->getElementType();
26040237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer  return Qs.apply(QT);
26055e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
26065e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
2607fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders CarlssonQualType ASTContext::getBaseElementType(const ArrayType *AT) {
2608fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson  QualType ElemTy = AT->getElementType();
26091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2610fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson  if (const ArrayType *AT = getAsArrayType(ElemTy))
2611fbbce49c116aa8c8c7c0707cb6048b55f70461a9Anders Carlsson    return getBaseElementType(AT);
26121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26136183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
26146183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
26156183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
26160de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
26171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
26180de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
26190de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
26200de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
26210de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
26220de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
26230de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
26240de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
26250de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
26260de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
26275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
26285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
2629a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
2630183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
26315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
2632a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2633183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2634183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
2635a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
26365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
26375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
26385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
26395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
26405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
26431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
2644716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
2645716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
26461361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
26471361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
26481361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
26491361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
26501361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
2651716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
2652f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
2653f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
2654f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
2655f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
2656f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
26571361b11066239ea15764a2a844405352d87296b3Chris Lattner
26581361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
26591361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
26601361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
26611361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
26621361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
26631361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
26645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
26655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
26687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
26697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
26701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
2671a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2672a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
2673a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
26741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2675a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
2676fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
2677a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
2678fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
2679fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
26805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2682f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2683f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
2684f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
2685f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
2686467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
2687f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
2688842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    T = ET->getDecl()->getPromotionType().getTypePtr();
2689f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2690a34267595534a72703290153a6f7e3da1adcec59Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::WChar))
2691a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
2692a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
2693f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
2694f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2695f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2696f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
2697f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2698f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2699f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
27007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
27017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
2702f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
27037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
27047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
27057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
27067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
2707f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
27087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
27097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
2710f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
27117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
27127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
2713f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
27147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
27157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
2716f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
27177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
27187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
2719f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
27202df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
27212df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
27222df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
2723f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
2724f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
2725f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
272604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
272704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
272804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
272904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
273004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
273104e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) {
2732ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
2733ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
2734ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
273504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
273604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
273704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
273804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
273904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
274004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
274104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
274204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
274304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
274404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
274504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
274604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
274704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
274804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
274904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
275004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
275104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
275204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
275304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
275404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
275504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
275604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
275704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
275804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
275904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
2760a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
2761a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2762a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
2763a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2764a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
2765a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
2766842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
2767842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
2768a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
2769a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
2770a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
2771a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
2772a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2773a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2774a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
2775a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
27761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
27777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
27781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
27797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
2780f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
2781f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
27827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
27831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2784f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2785f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
27861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
27887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
27891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
27917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
27927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
27937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
27941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
27967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
27977cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
27987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
27997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
28001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
28027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
28031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
28047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
28057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
28067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
28077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
28087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
28097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
28101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
28127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
28131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
28147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
28155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
281671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
281779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
281879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders CarlssonCreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
281979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                 SourceLocation L, IdentifierInfo *Id) {
282079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  if (Ctx.getLangOptions().CPlusPlus)
282179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson    return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
282279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
282379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson    return RecordDecl::Create(Ctx, TK, DC, L, Id);
282479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
282579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
28261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
282771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
282871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
28291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
2830465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
283179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
28325cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
283379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
2834f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
28351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
283671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
28370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
2838f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2839f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
284071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
28410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
284271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
28431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
28441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
284544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
284644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
28471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
284844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
2849a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
28501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
28514afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
28522888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
285317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
285444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
285544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
2856838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
285771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
28581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
285971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
28608467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2861b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2862319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
28636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2864319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2865319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2866319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2867319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
28682bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian// getNSConstantStringType - Return the type used for constant NSStrings.
28692bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz JahanianQualType ASTContext::getNSConstantStringType() {
28702bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  if (!NSConstantStringTypeDecl) {
28712bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl =
2872465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
28732bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                     &Idents.get("__builtin_NSString"));
28742bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->startDefinition();
28752bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28762bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    QualType FieldTypes[3];
28772bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28782bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const int *isa;
28792bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[0] = getPointerType(IntTy.withConst());
28802bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const char *str;
28812bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[1] = getPointerType(CharTy.withConst());
28822bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // unsigned int length;
28832bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[2] = UnsignedIntTy;
28842bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28852bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // Create fields
28862bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    for (unsigned i = 0; i < 3; ++i) {
28872bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
28882bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           SourceLocation(), 0,
28892bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           FieldTypes[i], /*TInfo=*/0,
28902bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*BitWidth=*/0,
28912bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*Mutable=*/false);
28922888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
28932bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      NSConstantStringTypeDecl->addDecl(Field);
28942bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    }
28952bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28962bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->completeDefinition();
28972bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  }
28982bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
28992bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  return getTagDeclType(NSConstantStringTypeDecl);
29002bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
29012bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
29022bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanianvoid ASTContext::setNSConstantStringType(QualType T) {
29032bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  const RecordType *Rec = T->getAs<RecordType>();
29042bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  assert(Rec && "Invalid NSConstantStringType");
29052bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  NSConstantStringTypeDecl = Rec->getDecl();
29062bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
29072bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
29081eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getObjCFastEnumerationStateType() {
2909bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
291044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
2911465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
291279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__objcFastEnumerationState"));
29135cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    ObjCFastEnumerationStateTypeDecl->startDefinition();
29141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2915bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2916bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2917de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
2918bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2919bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2920bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2921bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
29221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
292344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
29241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this,
29251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           ObjCFastEnumerationStateTypeDecl,
29261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           SourceLocation(), 0,
2927a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
29281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
29294afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
29302888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
293117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
293244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
29331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2934838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition();
2935bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
29361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2937bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2938bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2939bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2940adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike StumpQualType ASTContext::getBlockDescriptorType() {
2941adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
2942adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
2943adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2944adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
2945adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
2946465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
294779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
29485cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
2949adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2950adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
2951adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
2952adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
2953adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
2954adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2955adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
2956adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
2957083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
2958adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
2959adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2960adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
2961adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    FieldDecl *Field = FieldDecl::Create(*this,
2962adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         T,
2963adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
2964adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
2965a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
2966adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
2967adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*Mutable=*/false);
29682888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
2969adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
2970adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
2971adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2972838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
2973adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2974adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
2975adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2976adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
2977adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
2978adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2979adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stumpvoid ASTContext::setBlockDescriptorType(QualType T) {
2980adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const RecordType *Rec = T->getAs<RecordType>();
2981adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  assert(Rec && "Invalid BlockDescriptorType");
2982adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = Rec->getDecl();
2983adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
2984adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
2985083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike StumpQualType ASTContext::getBlockDescriptorExtendedType() {
2986083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
2987083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
2988083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2989083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
2990083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
2991465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
299279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
29935cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
2994083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
2995083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
2996083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
2997083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
2998083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
2999083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
3000083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3001083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3002083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
3003083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
3004083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
3005083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
3006083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
3007083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3008083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3009083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
3010083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    FieldDecl *Field = FieldDecl::Create(*this,
3011083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         T,
3012083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
3013083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
3014a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3015083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
3016083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*Mutable=*/false);
30172888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3018083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
3019083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
3020083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3021838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3022083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3023083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
3024083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3025083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
3026083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
3027083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3028083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stumpvoid ASTContext::setBlockDescriptorExtendedType(QualType T) {
3029083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const RecordType *Rec = T->getAs<RecordType>();
3030083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  assert(Rec && "Invalid BlockDescriptorType");
3031083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = Rec->getDecl();
3032083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
3033083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3034af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stumpbool ASTContext::BlockRequiresCopying(QualType Ty) {
3035af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isBlockPointerType())
3036af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3037af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (isObjCNSObjectType(Ty))
3038af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3039af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isObjCObjectPointerType())
3040af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3041af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
3042af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
3043af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3044af7b44d847d3e44c43346d508b2e55a6254b6e9dMike StumpQualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3045af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
3046ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
3047af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
3048ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
3049ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
305038e1627e895cd276186ebf177f75ba5d229c3fd7Mike Stump  //    void *__copy_helper;		// as needed
305138e1627e895cd276186ebf177f75ba5d229c3fd7Mike Stump  //    void *__destroy_help		// as needed
3052af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
3053ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
3054ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3055af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3056af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3057af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
30584d0d85c3370f2726c74ba0ece0a5e712830a1d82Fariborz Jahanian  static unsigned int UniqueBlockByRefTypeID = 0;
3059f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3060f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3061f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
3062af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
3063465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
306479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get(Name.str()));
3065af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
3066af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
3067af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3068af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
3069af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3070af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
3071af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3072af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3073af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3074af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3075af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
3076af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3077af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3078af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  const char *FieldNames[] = {
3079af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
3080af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
3081af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
3082af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
3083af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
3084af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
3085af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
3086af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3087af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3088af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
3089af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
3090af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
3091af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3092af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
3093a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3094af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
30952888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3096af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
3097af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
3098af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3099838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3100af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3101af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
3102ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
3103ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3104ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3105ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike StumpQualType ASTContext::getBlockParmType(
3106083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  bool BlockHasCopyDispose,
3107ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  llvm::SmallVectorImpl<const Expr *> &Layout) {
3108ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
3109adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Move up
31104d0d85c3370f2726c74ba0ece0a5e712830a1d82Fariborz Jahanian  static unsigned int UniqueBlockParmTypeID = 0;
3111f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3112f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__block_literal_"
3113f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  << ++UniqueBlockParmTypeID;
3114adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3115465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
311679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get(Name.str()));
31175cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3118adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3119adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    getPointerType(VoidPtrTy),
3120adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    IntTy,
3121adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    IntTy,
3122adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    getPointerType(VoidPtrTy),
3123083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    (BlockHasCopyDispose ?
3124083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump     getPointerType(getBlockDescriptorExtendedType()) :
3125083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump     getPointerType(getBlockDescriptorType()))
3126adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3127adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3128adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3129adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__isa",
3130adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__flags",
3131adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__reserved",
3132adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__FuncPtr",
3133adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "__descriptor"
3134adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3135adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3136adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 5; ++i) {
3137ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3138adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3139a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3140ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
31412888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3142ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    T->addDecl(Field);
3143ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  }
3144ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3145ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  for (unsigned i = 0; i < Layout.size(); ++i) {
3146ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    const Expr *E = Layout[i];
3147ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
3148ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    QualType FieldType = E->getType();
3149ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    IdentifierInfo *FieldName = 0;
3150ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    if (isa<CXXThisExpr>(E)) {
3151ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      FieldName = &Idents.get("this");
3152ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3153ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump      const ValueDecl *D = BDRE->getDecl();
3154ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      FieldName = D->getIdentifier();
3155ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      if (BDRE->isByRef())
3156ea1471e0e967548c596a71469702f8846dbaf3c0John McCall        FieldType = BuildByRefType(D->getNameAsCString(), FieldType);
3157ea1471e0e967548c596a71469702f8846dbaf3c0John McCall    } else {
3158ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      // Padding.
3159ea1471e0e967548c596a71469702f8846dbaf3c0John McCall      assert(isa<ConstantArrayType>(FieldType) &&
3160ea1471e0e967548c596a71469702f8846dbaf3c0John McCall             isa<DeclRefExpr>(E) &&
3161ea1471e0e967548c596a71469702f8846dbaf3c0John McCall             !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3162ea1471e0e967548c596a71469702f8846dbaf3c0John McCall             "doesn't match characteristics of padding decl");
3163ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    }
3164ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3165ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3166ea1471e0e967548c596a71469702f8846dbaf3c0John McCall                                         FieldName, FieldType, /*TInfo=*/0,
3167ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
31682888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3169adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3170adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3171adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3172838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3173ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3174ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  return getPointerType(getTagDeclType(T));
3175adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3176adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3177319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
31786217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3179319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3180319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3181319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3182319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
3183e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
3184e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
31852d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
3186e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
3187bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3188bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
31891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
319085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
319185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
319285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3193a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
319433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
3195aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen DyckCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
3196199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
31971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
319833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
3199199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  if (sz.isPositive() && type->isIntegralType())
3200199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
320133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
320233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
3203199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
3204aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
3205199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
3206199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
3207199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
3208199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
3209199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
321033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
321133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
32126f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian/// getObjCEncodingForBlockDecl - Return the encoded type for this block
32135e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
32145e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnallvoid ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
32155e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall                                             std::string& S) {
32165e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
32175e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
32185e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
32195e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
32205e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
32215e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
32225e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
32235e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
32245e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
3225199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3226199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
32276f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
32285e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
32295e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
3230aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3231199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
32325e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
32335e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
32345e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
3235199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
32365e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
32375e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
32385e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
32395e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
32405e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
32415e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
32425e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
32435e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
32445e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
32455e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
32465e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
32475e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
32485e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
32495e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
32505e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
32515e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
32525e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
32535e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
32545e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
3255199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3256aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
32575e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
32585e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
32595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
3260a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
326133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
32621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
3263e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
3264c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3265ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
3266a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
326733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
32680d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
326933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
327033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
327133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
327233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
3273199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
327433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
327533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
3276199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
327789951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
32787732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
327989951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
3280aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3281199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
3282199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
328333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
328433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
3285199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
328633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
3287199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
32881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
328933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
329033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
329189951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
32927732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
329389951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
32941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
32954306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
3296ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3297ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
3298ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
3299bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
3300ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
3301ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
3302ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
3303ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
330433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
33054306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
33060d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
3307199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3308aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
330933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
331033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
331133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
3312c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
331383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
3314c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3315c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
33161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
33171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
33181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
33191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
33201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
332183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
332283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
332383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
332483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
332583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
332683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
332783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
332883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
332983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
333083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
333183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
333283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
333383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
333483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
333583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
333683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
33371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
3338c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
3339e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
3340c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
3341c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
3342c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
3343c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3344c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
3345c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
33461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
3347c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
3348c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
334917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
3350653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
3351c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
3352c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
3353c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3354c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
3355c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
3356c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
3357c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
3358c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
3359c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
3360c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
336161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
3362c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
336317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
3364653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
3365c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
3366c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
3367c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3368c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
3369c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
3370c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
3371c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
3372c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
33731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3374c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
3375c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3376c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3377c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3378c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
3379c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3380c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
3381090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
3382090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
33831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
3384090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
3385090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
3386c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3387c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
3388c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
3389c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
3390c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
3391c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
3392c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
33931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
3394c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
3395c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3396c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3397c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
3398c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
3399c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
3400c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
3401c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3402090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3403090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
34041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3405c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3406c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
3407077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
3408c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3409c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3410c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3411c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
3412077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
3413c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3414c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3415c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
3416c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3417c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
341839f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
3419c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
3420c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3421c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
3422c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
3423c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3424a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
34251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
34261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
3427a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3428a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
3429a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
34308e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
3431183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
3432c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
3433c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
3434a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
34351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
3436c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
3437c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
3438a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
3439a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
3440a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
3441a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
3442a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
34437d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
3444153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
344582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
344682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
344782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
344882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
34491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
34505b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
345182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
345282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
34531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void EncodeBitField(const ASTContext *Context, std::string& S,
3454153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                           const FieldDecl *FD) {
34558b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
34568b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
34578b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
34589a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
34598b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
34608b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
34618b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
34628b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
346301eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
346482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
346582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
346682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
3467153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
3468090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
34696ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
3470183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
3471ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
3472ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      return EncodeBitField(this, S, FD);
3473ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    char encoding;
3474ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    switch (BT->getKind()) {
34751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: assert(0 && "Unhandled builtin type kind");
3476ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Void:       encoding = 'v'; break;
3477ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Bool:       encoding = 'B'; break;
3478ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_U:
3479ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UChar:      encoding = 'C'; break;
3480ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UShort:     encoding = 'S'; break;
3481ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt:       encoding = 'I'; break;
34821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::ULong:
34831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        encoding =
34841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
348572696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        break;
3486ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt128:    encoding = 'T'; break;
3487ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULongLong:  encoding = 'Q'; break;
3488ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_S:
3489ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::SChar:      encoding = 'c'; break;
3490ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Short:      encoding = 's'; break;
3491ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int:        encoding = 'i'; break;
34921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::Long:
34931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      encoding =
34941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
3495ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      break;
3496ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongLong:   encoding = 'q'; break;
3497ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int128:     encoding = 't'; break;
3498ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Float:      encoding = 'f'; break;
3499ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Double:     encoding = 'd'; break;
3500ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongDouble: encoding = 'd'; break;
350143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
35021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3503ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += encoding;
3504ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3505ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
35061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3507183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
3508c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
35091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
3510c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
3511ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3512ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
351360bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
3514aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
3515aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
35166217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
35178d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
35188d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
35198d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
35208d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
3521aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
3522aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
3523aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3524aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
3525aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
3526a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
3527a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
3528a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
3529a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
35301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
35318e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
3532a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
3533a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3534a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3535a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
35369fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
3537a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
35386217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
35396217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
3540a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
3541a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3542a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3543a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
3544a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
3545a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
3546a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
3547a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
3548a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
35490237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      if (llvm::StringRef(S).endswith("nr"))
35500237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
3551a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
35521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
355385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
355485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
355585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
3556e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
355785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
355885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
355985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
35606217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
35619533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
35629533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
35639533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
35649533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
35659533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
35669533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
35679533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
35689533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
35699533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
35709533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
35719533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
357285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
357385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
3574a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
3575a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
35761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
357743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
3578ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3579ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3580aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
3581ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
3582ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
3583ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
3584559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
3585559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
3586559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
3587559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
35881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3589559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3590559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
3591559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
35921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3593559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3594559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
3595559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
3596559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
3597559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3598559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
3599559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
36001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3602559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3603559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
3604559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
3605ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3606ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3608183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
3609c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
3610ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3611ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36136217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
361482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
3615d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
3616502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
3617502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3618502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
36196fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
36206fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
36216fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
36226fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
36236fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
36246fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            TemplateArgs.getFlatArgumentList(),
36256fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            TemplateArgs.flat_size(),
36266fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            (*this).PrintingPolicy);
36276fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
36286fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
36296fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
3630502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
3631502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
3632502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
36330d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
36347d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
363517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
363617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   FieldEnd = RDecl->field_end();
363744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
363843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
3639d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
364044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
3641d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
3642d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
36431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3644d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
364543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
36461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
364743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
3648d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
3649a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
3650a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
36511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(qt, S, false, true,
365243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
3653d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
36547d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
36556de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
3656d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
3657ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3658ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3660ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
36618b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
36628b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
36638b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
36648b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
3665ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3666ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3668ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
366921a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
3670ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3671ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
36721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3673c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
3674c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3675c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
3676c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
36770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
367843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
36790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
368043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
368143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
368243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
368343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
3684f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
368543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
3686f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
368743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
36881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
368943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
369043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
36911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
369243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
369343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
369443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
3695ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
369643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
36971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3698183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
369914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
370014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
370114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3702ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
37031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
370427d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
370527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
370627d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
370727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
370814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
370914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3710ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
37111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3712ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
37131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
371414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
371514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
371614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
371714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
371814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
371914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
372067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
372167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
372214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
372314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
372414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
372514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
372614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
372714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
372814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3729ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
37301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3731ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
3732ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
3733ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
3734ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
37351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
3736ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
3737ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
37381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
37391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
3740ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
374114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
374214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
3743ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3744ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
374527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
3746ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
374727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
374867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
374967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
3750ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
3751ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
3752ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
37531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3754ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
3755ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3756ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3757ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
37581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3759532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
3760532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
3761532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
3762532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
3763532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall
3764ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
376585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
376685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
37671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
3768ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
3769ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
3770ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
3771ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
3772ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
3773ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
3774ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
3775ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
3776ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
3777ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
3778ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
3779ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
3780ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
3781ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
3782ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
3783ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
3784b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
37851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3786b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
3787b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
3788b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3789ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
3790de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
37917e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
37927e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
3793ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
379413dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = T;
3795b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
3796b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3797ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
3798a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
3799390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
3800390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3801ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
3802de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
38038baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
38048baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
3805a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
38061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
38072198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
38081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3809a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
38102198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
38112198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
38120bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
38130bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
3814eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCallTemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3815eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall                                                   UnresolvedSetIterator End) {
38160bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
38170bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
38187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38190bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
38200bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
38210bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
38220bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
38230bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
3824eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
38250bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
38260bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
38270bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
38280bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
38290bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
38307532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
38317532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38320bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
38337532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
38347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3835d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
3836d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
38371eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
3838d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                                  bool TemplateKeyword,
38390bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                                                  TemplateDecl *Template) {
3840789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
3841d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
3842d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
38431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3844d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
3845d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
38460bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3847d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
3848d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3849d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3850d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
38511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3852d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
3853d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
3854d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
38557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
38567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
38571eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
38587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
38591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
38603b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
38617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
38637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
38647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
38667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
38677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
38687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
38707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
38717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
38737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
38747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
38757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
38767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
38777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3878789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
3879789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3880789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
3881789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
38827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
38837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
38847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
38857532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
38867532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
38877532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3888ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
3889ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
3890ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
3891ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3892ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor                                     OverloadedOperatorKind Operator) {
3893ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
3894ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
3895ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3896ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
3897ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
3898ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3899ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
3900789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
3901789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3902ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3903ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
3904ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
3905ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3906ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3907ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
3908ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3909ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
3910ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3911ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
3912789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3913789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
3914789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3915789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
3916789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
3917ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
3918ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3919ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
3920ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
3921ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
3922ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
3923b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
3924d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
3925d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
3926e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
3927b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
3928e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
3929b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
3930b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
3931b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
3932b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
3933b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
3934b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
3935b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
3936b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3937b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
3938b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
3939b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
3940e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  return CanQualType();
3941b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
3942b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3943b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3944b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
3945b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3946b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3947fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
3948fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
3949fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
3950f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
3951fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
3952fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
3953fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3954fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
395540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
3956fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
3957fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
39581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
3959fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
3960fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
39614fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
39624fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
39634fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
39640953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
39650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttrs = Qualifiers::GCNone;
39664fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
39674fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
3968b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
39694fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
39701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // (or pointers to them) be treated as though they were declared
3971a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
39720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GCAttrs == Qualifiers::GCNone) {
397375212ee91313bc1b6dd826d9b173541bc4016539Fariborz Jahanian      if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
39740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        GCAttrs = Qualifiers::Strong;
3975a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
39766217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
3977a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
3978c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
3979c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
3980f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
39810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return Qualifiers::GCNone;
39824fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
3983b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
39844fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
39854fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
39866ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
39876ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
39886ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
3989770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
39901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
39916ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
39926ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
39936ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
3994467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
39956ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
399661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
39976ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
39986ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
39994084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
40004084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
40014084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
40024084c306635b70f37029dca938444e6013f08684Steve Naroff
40034084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
40044084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
40050fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
40060fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian                                                ObjCProtocolDecl *rProto) {
40074084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
400814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
40094084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
40104084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
40114084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
40124084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
40134084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
40144084c306635b70f37029dca938444e6013f08684Steve Naroff}
401514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
40164084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
40174084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
40184084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
40194084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
40204084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
40214084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
40224084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
40234084c306635b70f37029dca938444e6013f08684Steve Naroff}
40244084c306635b70f37029dca938444e6013f08684Steve Naroff
40254084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
40264084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
40274084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
40284084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
40294084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
40301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
40314084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
40324084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
40331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
40344084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
40354084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
40364084c306635b70f37029dca938444e6013f08684Steve Naroff
40374084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
4038183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
40391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40404084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
40411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40424084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
40431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
40444084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
40454084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
40464084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40474084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->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 (!rhsID->ClassImplementsProtocol(*I, true))
40524084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
40534084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40544084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40554084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
40564084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
40574084c306635b70f37029dca938444e6013f08684Steve Naroff    }
40581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
40594084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40604084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
40614084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
40624084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
4063de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
4064de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
4065de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
4066de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
40674084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
40684084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
40694084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
40704084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
40714084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
40724084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
40738f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
40748f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
4075de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
40761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
40774084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
40784084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
40794084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
40804084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
40814084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
40824084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
40834084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
40840fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
40854084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
40864084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
40874084c306635b70f37029dca938444e6013f08684Steve Naroff          }
40884084c306635b70f37029dca938444e6013f08684Steve Naroff        }
40894084c306635b70f37029dca938444e6013f08684Steve Naroff      }
40904084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
4091de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
4092de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
40931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4094de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
4095de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
40961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40974084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
40984084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
40994084c306635b70f37029dca938444e6013f08684Steve Naroff
41001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
41014084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
41024084c306635b70f37029dca938444e6013f08684Steve Naroff    if (lhsOPT->qual_empty()) {
41034084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
41044084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
41054084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
41064084c306635b70f37029dca938444e6013f08684Steve Naroff             E = rhsQID->qual_end(); I != E; ++I) {
41074084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
41084084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
41094084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
41100fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (lhsID->ClassImplementsProtocol(*I, true)) {
41114084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
41124084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
41134084c306635b70f37029dca938444e6013f08684Steve Naroff          }
41144084c306635b70f37029dca938444e6013f08684Steve Naroff        }
41154084c306635b70f37029dca938444e6013f08684Steve Naroff        if (!match)
41164084c306635b70f37029dca938444e6013f08684Steve Naroff          return false;
41174084c306635b70f37029dca938444e6013f08684Steve Naroff      }
41184084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
41194084c306635b70f37029dca938444e6013f08684Steve Naroff    }
41201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
41214084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
41224084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
41234084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
41244084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
41254084c306635b70f37029dca938444e6013f08684Steve Naroff
41264084c306635b70f37029dca938444e6013f08684Steve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
41274084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
41284084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
41294084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
41304084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
41314084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
41324084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
41334084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
41344084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
41354084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
41364084c306635b70f37029dca938444e6013f08684Steve Naroff        }
41374084c306635b70f37029dca938444e6013f08684Steve Naroff      }
41384084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
41394084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
41404084c306635b70f37029dca938444e6013f08684Steve Naroff    }
41414084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
41424084c306635b70f37029dca938444e6013f08684Steve Naroff  }
41434084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
41444084c306635b70f37029dca938444e6013f08684Steve Naroff}
41454084c306635b70f37029dca938444e6013f08684Steve Naroff
41464084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
41474084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
41484084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
41494084c306635b70f37029dca938444e6013f08684Steve Naroff///
41504084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
41514084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
4152c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
4153c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
4154c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
41554084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
4156c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
4157c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
41584084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
41594084c306635b70f37029dca938444e6013f08684Steve Naroff
4160c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
41611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
41621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
41634084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
41644084c306635b70f37029dca938444e6013f08684Steve Naroff
4165c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
4166c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
41674084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
41681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41694084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
417014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
417114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
4172132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4173132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// for providing type-safty for objective-c pointers used to pass/return
4174132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
4175132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4176132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
4177132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
4178132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
4179132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT) {
4180a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
4181132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
4182132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4183132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
4184132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4185132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
4186132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4187a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
4188132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4189132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
4190132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
4191132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4192132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4193132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4194132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
4195132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
4196132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4197132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian        return false;
4198132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4199132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian        return true;
4200132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4201132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
4202132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
4203132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
4204132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
4205132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
4206132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4207e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
4208e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
4209e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
4210e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
4211e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
4212e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
4213e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
4214e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
4215e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4216e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4217c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
4218c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
4219c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
4220c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
4221e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4222e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4223e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
4224e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
4225e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4226e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
4227432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4228c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
4229c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
4230e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4231e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
4232e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4233e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4234e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
4235e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
4236cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
4237cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
4238e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
4239e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
4240e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
4241e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4242e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
4243432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
4244c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
4245c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
4246432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4247432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
4248432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
4249432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
4250432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
4251e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
4252e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
4253e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
4254db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
4255db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
4256db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
4257db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
4258db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
4259c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
4260c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
4261c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
4262c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
4263c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4264c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4265c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LDecl || !RDecl)
4266db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
4267db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4268c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  while ((LDecl = LDecl->getSuperClass())) {
4269c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
4270e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
4271c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4272c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4273c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
4274c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
4275c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
4276c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4277c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
4278c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
4279e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
4280db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  }
4281db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4282db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
4283db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
4284db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
4285c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4286c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
4287c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
4288c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
4289c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
42906ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
42916ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
4292c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
42936ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
42941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42956ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
42966ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
4297c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
42986ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
42991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43006ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
43016ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
4302c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (RHS->getNumProtocols() == 0)
43036ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
43041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4305c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4306c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
430791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
430891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
430991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
431091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
431191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
4312c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4313c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
43148f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
43158f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
431691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
43178f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
43188f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
431991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
432091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
432191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
432291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
432391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
432491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
432591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
43266ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
43276ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
4328389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4329389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
4330183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4331183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
43321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
433314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
4334389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
433514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
433614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
433714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
4338389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
4339389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
43401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
4341ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
43421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
4343ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
43443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
43450e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  if (getLangOptions().CPlusPlus)
43460e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
43470e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
43483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
43493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
43503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4351132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4352132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
4353132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
4354132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
4355132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4356132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                        bool OfBlockPointer) {
4357183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
4358183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
435972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
436072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
43613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
43623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
43633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
43643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
4365132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
4366132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (OfBlockPointer)
4367132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4368132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
4369132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian   retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
43703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
43716de8b62ae42818c0bd8878b0d1900e17c7c43e0eFariborz Jahanian  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
437261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
43736de8b62ae42818c0bd8878b0d1900e17c7c43e0eFariborz Jahanian  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
437461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
43756a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
43766a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
43776a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
43786a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
4379264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4380264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
43816a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
43826a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      lbaseInfo.getRegParm();
43836a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
43846a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  if (NoReturn != lbaseInfo.getNoReturn() ||
43856a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      RegParm != lbaseInfo.getRegParm())
43866a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
43876a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  if (NoReturn != rbaseInfo.getNoReturn() ||
43886a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar      RegParm != rbaseInfo.getRegParm())
43896a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
4390264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CallingConv lcc = lbaseInfo.getCC();
4391264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CallingConv rcc = rbaseInfo.getCC();
4392ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  // Compatible functions must have compatible calling conventions
439304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isSameCallConv(lcc, rcc))
4394ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor    return QualType();
43951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
4397465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4398465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
43993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
44003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
44013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
44033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
44043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
44053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
44073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
44083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
44093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44107fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
44117fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
44127fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
44133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
44143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
44153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
44163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
44173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
4418132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
44193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
44203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
442161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
442261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
442361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
442461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
44253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
44263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
44273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
44283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
44292455636163fdd18581d7fdae816433f886d88213Mike Stump                           lproto->isVariadic(), lproto->getTypeQuals(),
4430264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           false, false, 0, 0,
4431425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                           FunctionType::ExtInfo(NoReturn, RegParm, lcc));
44323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
44333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
44353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
44363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
443772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
44383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
4439465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
44403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
44413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
44423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
44433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
44443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
44453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
44463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
44473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
44483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
4449b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
4450b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
4451b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
4452b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
4453b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
4454b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
44553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
44563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
44573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
44583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
44593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
44613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
44623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
44632d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getNumArgs(), proto->isVariadic(),
4464264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           proto->getTypeQuals(),
4465264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                           false, false, 0, 0,
4466425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                           FunctionType::ExtInfo(NoReturn, RegParm, lcc));
44673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
44683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
44703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
4471425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
4472264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getFunctionNoProtoType(retType, Info);
44733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
44743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4475132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4476132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                bool OfBlockPointer) {
447743d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
447843d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
447943d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
44807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
44817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
44820e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
44830e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
44840e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
44853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
44863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
44873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4488f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
44893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
44903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
44913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
44920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
4493a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
4494a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
44950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
44960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
44970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
44980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
44990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace())
45000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
45010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
45020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
45030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
45040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
45050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
45060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
45070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
45080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
45090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
45100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
45110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
45120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
45130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
45140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
45150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
45160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
45170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
45180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
45190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
45203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
45210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
45220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
45230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
45243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
4525852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
4526852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
4527f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
45281adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
45291adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
45301adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
45311adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
45324c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
45334c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
4534a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4535a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
4536a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4537a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
45381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4539c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
4540c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4541c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4542c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
4543213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
4544213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4545213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
45461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45474e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
45484e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
45491adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
45501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
4551842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
4552842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
4553183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
45543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
45553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
4556bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
4557183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
45583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
45593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
4560bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
45611adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
45623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
4563ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
45643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
45654a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
45661adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
456772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
456872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
4569ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
457072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
457172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
457272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
457372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
457472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
457572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
45767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
45777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
457872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
457972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
458072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
458172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
4582c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
458372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
458472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
458572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
458672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
458772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
458872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
458972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
45901adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
45913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
45923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
45936217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
45946217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
45953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
45963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
459707d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
459861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
459907d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
460061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
46013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
46023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
4603c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
4604c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
4605c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
46066217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
46076217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
4608132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
4609c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
4610c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4611c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
4612c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4613c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
4614c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
4615c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
46161adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
46173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
46183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
46193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
46203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
46213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
46223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
46233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
46243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
46253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
46263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
462761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
462861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
462961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
463061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
46313bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
46323bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
46333bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
46343bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
46353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
46363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
463761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
463861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
463961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
464061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
46413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
46423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
46433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
46443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
46453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
46463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
46473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
46483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
46493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
46503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
46513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
46523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
46533bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
46543bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
46557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
46567e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
46573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
46581adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
4659132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
466072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
466172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
46623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
46631adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
46643cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
46653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
466664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
466764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
466864cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
46693cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
46705a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
46711c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
46721c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
46733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
467461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
4675c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
4676c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
46775a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
46785a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
4679c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
4680c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
4681c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
46825fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
46835fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
46843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
468561490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
468614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
4687132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
4688132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
4689132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
4690132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>()))
4691132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return LHS;
4692132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
4693132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4694183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4695183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
469614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
469714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
4698bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
4699132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
4700ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
470172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
470272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
4703ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
47047192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
47052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
47062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
47072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
47082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
47092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
47102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
47112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
47122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
47132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
47142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
47152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
47162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
47172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
47182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
47192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
47202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
47212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
47222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
47232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
47242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
47252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
47262390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
47272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
47282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
47292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
47302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
47312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
47322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
47332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->getNumArgs(), FPT->isVariadic(),
47342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->getTypeQuals(),
47352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->hasExceptionSpec(),
47362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->hasAnyExceptionSpec(),
47372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->getNumExceptions(),
47382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  FPT->exception_begin(),
47392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian                                  Info);
47402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
47412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
47422390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
47432390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
47442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
47452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47462390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
47472390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
47482390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
47492390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
47502390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
47512390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
47522390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
47532390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
47542390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47552390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
47562390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
47572390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
47582390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
47592390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
47602390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
47612390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
47622390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
47632390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47642390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
47652390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
47662390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47672390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
47682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
47692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
47702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
47712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
47722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
47732390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47742390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
47752390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
47762390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
47772390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
47782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
47792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
47802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
47812390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
47822390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
47832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
47842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
47852390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
47865426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
4787ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
4788ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
478988054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
4790ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
4791632d772a78db7e2cd9b36f8a22aee49d44486fbfSebastian Redl  if (T->isBooleanType())
4792ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
4793842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (EnumType *ET = dyn_cast<EnumType>(T))
479429a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
4795f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
4796ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
4797ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4798ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
4799ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4800ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
48016a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
48026a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
48036a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
48046a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
480582287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson             VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
48066a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
48076a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
48086a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
4809ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
48106a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
48116a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
48126a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
4813ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
4814ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
4815ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
4816ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
4817ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
4818ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
4819ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
4820ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
4821ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
4822ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
4823ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
4824ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
48252df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
48262df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
4827ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
4828ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
4829ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
4830ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
4831ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4832ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
48332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
48342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
48352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
483686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
483786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
483886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
483986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
484086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
484186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
484286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
484386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters.  This returns the resultant type.
48441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
484586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
484686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  bool AllowTypeModifiers = true) {
484786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
484886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
484986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
48501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
485186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the modifiers first.
485286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
485386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
485486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
48551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
485686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
485786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
485886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
485986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
486086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
486186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
486286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
486386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
486486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
486586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
486686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
486786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
486886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
486986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
487086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
487186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
487286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
487386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
48741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
487586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
487686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
487786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
487886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
487986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
488086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
488186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
488286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
488386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
488486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
488586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
488686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
488786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
488886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
488986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
489086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
489186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
489286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
489386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
489486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
489586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
489686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
489786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
489886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
489986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
490086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
490186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
490286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
490386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
490486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
490586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
490686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
490786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
490886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
490986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
491086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
491186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
491286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
491386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
491486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
491586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
491686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
491786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
491886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
491986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
492086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
492186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
492286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
492386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
492486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
492586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
492686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
492786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
492886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
492986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
493086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
493186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
493286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
493386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
493486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
493586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
493686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
493786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
493886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
493986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
494086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
494186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
494286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
494386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
494486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
494586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
494686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
494786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
494886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Type->isArrayType()) {
494986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
495086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    } else {
495186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
495286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
495386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
495486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
495586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
495686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
495786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
49581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
495986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
49601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
496186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
496282287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    // FIXME: Don't know what to do about AltiVec.
496382287d19ded35248c4ce6a425ce74116a13ce44eJohn Thompson    Type = Context.getVectorType(ElementType, NumElements, false, false);
496486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
496586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
4966d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
4967d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4968d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
4969d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
4970d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  }
49719a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
4972c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
4973c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
4974f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
497586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
497686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
4977fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
49789a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
4979f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
4980782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
4981f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
4982f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
4983f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
4984fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
4985f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
4986fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
4987fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
4988fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
4989782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
49901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
499186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (!AllowTypeModifiers)
499286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return Type;
49931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
499486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Done = false;
499586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
4996187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
499786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      default: Done = true; --Str; break;
499886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '*':
499986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '&':
5000187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        {
5001187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          // Both pointers and references can have their pointee types
5002187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          // qualified with an address space.
5003187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          char *End;
5004187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          unsigned AddrSpace = strtoul(Str, &End, 10);
5005187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          if (End != Str && AddrSpace != 0) {
5006187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall            Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5007187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall            Str = End;
5008187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          }
5009187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        }
5010187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        if (c == '*')
5011187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          Type = Context.getPointerType(Type);
5012187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall        else
5013187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall          Type = Context.getLValueReferenceType(Type);
501486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
501586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      // FIXME: There's no way to have a built-in with an rvalue ref arg.
501686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case 'C':
50170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Type = Type.withConst();
501886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
5019013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian      case 'D':
5020013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian        Type = Context.getVolatileType(Type);
5021013af39d2a6a2e684f5a458c97890c52cddb7157Fariborz Jahanian        break;
502286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
502386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
50241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
502686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
502786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
502886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
502986df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id,
503086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                    GetBuiltinTypeError &Error) {
503186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(id);
50321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
503386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
50341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
503586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
503686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
503786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
503886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
503986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
504086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
504186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
504286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
504386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
504486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
504586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
504686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
50471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
504886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
504986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
505086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
505186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
505286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
505386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
505486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
505586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (ArgTypes.size() == 0 && TypeStr[0] == '.')
505686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return getFunctionNoProtoType(ResType);
5057ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
5058ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor  // FIXME: Should we create noreturn types?
505986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
5060ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor                         TypeStr[0] == '.', 0, false, false, 0, 0,
5061264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                         FunctionType::ExtInfo());
506286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
5063a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
5064a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType
5065a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
5066a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Perform the usual unary conversions. We do this early so that
5067a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // integral promotions to "int" can allow us to exit early, in the
5068a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // lhs == rhs check. Also, for conversion purposes, we ignore any
5069a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // qualifiers.  For example, "const float" and "float" are
5070a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // equivalent.
5071a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isPromotableIntegerType())
5072a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = getPromotedIntegerType(lhs);
5073a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
5074a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = lhs.getUnqualifiedType();
5075a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (rhs->isPromotableIntegerType())
5076a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = getPromotedIntegerType(rhs);
5077a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
5078a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = rhs.getUnqualifiedType();
5079a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
5080a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If both types are identical, no conversion is needed.
5081a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs == rhs)
5082a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
50831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5084a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5085a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The caller can deal with this (e.g. pointer + int).
5086a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5087a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
50881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // At this point, we have two different arithmetic types.
50901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5091a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Handle complex types first (C99 6.3.1.8p1).
5092a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexType() || rhs->isComplexType()) {
5093a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the complex type.
50941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
5095a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
5096a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5097a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
50981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
5099a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
5100a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5101a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5102a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // This handles complex/complex, complex/float, or float/complex.
51031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // When both operands are complex, the shorter operand is converted to the
51041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // type of the longer, and that is the type of the result. This corresponds
51051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // to what is done when combining two real floating-point operands.
51061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // The fun begins when size promotion occur across type domains.
5107a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // From H&S 6.3.4: When one operand is complex and the other is a real
51081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // floating-point type, the less precise type is converted, within it's
5109a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // real or complex domain, to the precision of the other type. For example,
51101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // when combining a "long double" with a "double _Complex", the
5111a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // "double _Complex" is promoted to "long double _Complex".
5112a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
51131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (result > 0) { // The left side is bigger, convert rhs.
5115a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
51161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (result < 0) { // The right side is bigger, convert lhs.
5117a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
51181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
5119a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // At this point, lhs and rhs have the same rank/size. Now, make sure the
5120a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // domains match. This is a requirement for our implementation, C99
5121a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // does not require this promotion.
5122a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5123a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5124a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return rhs;
5125a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      } else { // handle "_Complex double, double".
5126a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs;
5127a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      }
5128a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5129a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs; // The domain/size match exactly.
5130a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5131a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Now handle "real" floating types (i.e. float, double, long double).
5132a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5133a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the real floating type.
5134a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isIntegerType()) {
5135a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the lhs floating point type.
5136a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5137a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5138a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isComplexIntegerType()) {
5139a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the complex floating point type.
5140a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(lhs);
5141a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5142a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isIntegerType()) {
5143a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the rhs floating point type.
5144a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5145a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
51461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isComplexIntegerType()) {
5147a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the complex floating point type.
5148a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(rhs);
5149a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5150a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // We have two real floating types, float/complex combos were handled above.
5151a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Convert the smaller operand to the bigger result.
5152a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
5153a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (result > 0) // convert the rhs
5154a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5155a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    assert(result < 0 && "illegal float comparison");
5156a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return rhs;   // convert the lhs
5157a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5158a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5159a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Handle GCC complex int extension.
5160a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5161a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5162a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
5163a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhsComplexInt && rhsComplexInt) {
51641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
5165a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman                              rhsComplexInt->getElementType()) >= 0)
5166a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs; // convert the rhs
5167a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5168a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (lhsComplexInt && rhs->isIntegerType()) {
5169a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
5170a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
5171a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (rhsComplexInt && lhs->isIntegerType()) {
5172a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
5173a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
5174a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
5175a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5176a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Finally, we have two differing integer types.
5177a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The rules for this case are in C99 6.3.1.8
5178a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  int compare = getIntegerTypeOrder(lhs, rhs);
5179a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  bool lhsSigned = lhs->isSignedIntegerType(),
5180a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman       rhsSigned = rhs->isSignedIntegerType();
5181a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  QualType destType;
5182a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhsSigned == rhsSigned) {
5183a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Same signedness; use the higher-ranked type
5184a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = compare >= 0 ? lhs : rhs;
5185a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (compare != (lhsSigned ? 1 : -1)) {
5186a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The unsigned type has greater than or equal rank to the
5187a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // signed type, so use the unsigned type
5188a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? rhs : lhs;
5189a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5190a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The two types are different widths; if we are here, that
5191a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // means the signed type is larger than the unsigned type, so
5192a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // use the signed type.
5193a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? lhs : rhs;
5194a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else {
5195a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The signed type is higher-ranked than the unsigned type,
5196a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // but isn't actually any bigger (like unsigned int and long
5197a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // on most 32-bit systems).  Use the unsigned type corresponding
5198a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // to the signed type.
5199a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5200a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
5201a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return destType;
5202a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
5203