ASTContext.cpp revision 0de78998e7bda473b408437053e48661b510d453
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
17aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
18b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
19e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2119cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
221b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
23a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
2585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
266fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
27557c5b1717bc8919b1b40cf2064b51491ec53a44Chris Lattner#include "llvm/Support/MemoryBuffer.h"
2829445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson#include "RecordLayoutBuilder.h"
2929445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3661710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
3761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner                       TargetInfo &t,
38e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
391b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
401b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       bool FreeMem, unsigned size_reserve) :
41ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
42782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
43782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
46e4f2142d00fa5fdb580c4e2413da91882d955381Chris Lattner  BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
470f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCIdRedefinitionType = QualType();
480f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCClassRedefinitionType = QualType();
49e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  if (size_reserve > 0) Types.reserve(size_reserve);
50e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
5114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
52e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
53e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Deallocate all the types.
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!Types.empty()) {
574b05b1dee6cc65ae61d93dab7edff72710f24589Ted Kremenek    Types.back()->Destroy(*this);
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Types.pop_back();
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
60b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
61b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
62b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
63b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
64b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
65b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
67b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
68b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
69b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
70b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
71b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar    llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
72b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar      I = ObjCLayouts.begin(), E = ObjCLayouts.end();
73b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
74b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
75b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
76b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
77b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
78b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
79ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
801ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
811ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
821ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNSEnd = NestedNameSpecifiers.end();
83e7dcd782df0e1cc3a63e429a866098e69db39153Douglas Gregor       NNS != NNSEnd;
841ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor       /* Increment in loop */)
851ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
86ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
87ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
88ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
89ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
90b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
1017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
102dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
103dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent) 0,
104dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
105dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
106dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
107dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
108c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
111dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
114dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
115dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
116dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
117dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
118dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
119dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
120dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
121dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
122dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
123dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor
124dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
134f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
14615b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
16864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
1692df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
1702df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
1712df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
1722df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
1733a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
1743a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
1753a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
1763a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
17764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
178f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
179f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
180f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
181f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
182f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
183f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
184f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
185f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
186f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
187f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
1888e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
189898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
190898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
191898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
192898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
193898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
194898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
195898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
196898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
1978e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
198e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // Placeholder type for C++0x auto declarations whose real type has
199e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // not yet been deduced.
200e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
201e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
2068e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2077e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
20814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
209de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
210de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
211de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
212de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
213de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Builtin types for 'id' and 'Class'.
214de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
215de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
21614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
217a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
21833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
21933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
22033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2216e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2226e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2236e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorVarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) {
2277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
2287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos
2297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
2307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
2317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
2327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
2347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid
2377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) {
2387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
2397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
2407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
2417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
2427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatedFromStaticDataMember[Inst] = Tmpl;
2437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace {
2462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  class BeforeInTranslationUnit
2472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    : std::binary_function<SourceRange, SourceRange, bool> {
2482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    SourceManager *SourceMgr;
2492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  public:
2512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
2522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    bool operator()(SourceRange X, SourceRange Y) {
2542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
2552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
2562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  };
2572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
2582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Determine whether the given comment is a Doxygen-style comment.
2602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
2612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Start the start of the comment text.
2622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
2632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param End the end of the comment text.
2642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
2652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Member whether we want to check whether this is a member comment
2662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
2672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// we only return true when we find a non-member comment.
2682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorstatic bool
2692e22253e03e175144aeb9d13350a12fd83f858beDouglas GregorisDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
2702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                 bool Member = false) {
2712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *BufferStart
2722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
2732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
2742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
2752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (End - Start < 4)
2772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
2782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  assert(Start[0] == '/' && "Not a comment?");
2802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
2812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
2822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
2832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
2842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return (Start[3] == '<') == Member;
2862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
2872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Retrieve the comment associated with the given declaration, if
2892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// it has one.
2902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorconst char *ASTContext::getCommentForDecl(const Decl *D) {
2912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!D)
2922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
2932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check whether we have cached a comment string for this declaration
2952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // already.
2962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  llvm::DenseMap<const Decl *, std::string>::iterator Pos
2972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = DeclComments.find(D);
2982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Pos != DeclComments.end())
2992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return Pos->second.c_str();
3002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If we have an external AST source and have not yet loaded comments from
3022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // that source, do so now.
3032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (ExternalSource && !LoadedExternalComments) {
3042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::vector<SourceRange> LoadedComments;
3052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ExternalSource->ReadComments(LoadedComments);
3062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!LoadedComments.empty())
3082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      Comments.insert(Comments.begin(), LoadedComments.begin(),
3092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                      LoadedComments.end());
3102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    LoadedExternalComments = true;
3122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
3132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If there are no comments anywhere, we won't find anything.
3152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Comments.empty())
3162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the declaration doesn't map directly to a location in a file, we
3192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // can't find the comment.
3202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  SourceLocation DeclStartLoc = D->getLocStart();
3212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
3222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Find the comment that occurs just before this declaration.
3252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator LastComment
3262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = std::lower_bound(Comments.begin(), Comments.end(),
3272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       SourceRange(DeclStartLoc),
3282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       BeforeInTranslationUnit(&SourceMgr));
3292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the location for the start of the declaration and find the
3312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // beginning of the file buffer.
3322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::pair<FileID, unsigned> DeclStartDecomp
3332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(DeclStartLoc);
3342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *FileBufferStart
3352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(DeclStartDecomp.first).first;
3362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // First check whether we have a comment for a member.
3382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment != Comments.end() &&
3392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
3402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      isDoxygenComment(SourceMgr, *LastComment, true)) {
3412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> LastCommentEndDecomp
3422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(LastComment->getEnd());
3432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
3442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor        SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
3452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor          == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
3462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                                     LastCommentEndDecomp.second)) {
3472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // The Doxygen member comment comes after the declaration starts and
3482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is on the same line and in the same file as the declaration. This
3492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is the comment we want.
3502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      std::string &Result = DeclComments[D];
3512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      Result.append(FileBufferStart +
3522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                      SourceMgr.getFileOffset(LastComment->getBegin()),
3532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                    FileBufferStart + LastCommentEndDecomp.second + 1);
3542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return Result.c_str();
3552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
3562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
3572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment == Comments.begin())
3592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  --LastComment;
3612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the end of the comment.
3632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::pair<FileID, unsigned> LastCommentEndDecomp
3642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(LastComment->getEnd());
3652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment and the declaration aren't in the same file, then they
3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // aren't related.
3682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartDecomp.first != LastCommentEndDecomp.first)
3692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check that we actually have a Doxygen comment.
3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!isDoxygenComment(SourceMgr, *LastComment))
3732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Compute the starting line for the declaration and for the end of the
3762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // comment (this is expensive).
3772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned DeclStartLine
3782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned CommentEndLine
3802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
3812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                              LastCommentEndDecomp.second);
3822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment does not end on the line prior to the declaration, then
3842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // the comment is not associated with the declaration at all.
3852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (CommentEndLine + 1 != DeclStartLine)
3862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // We have a comment, but there may be more comments on the previous lines.
3892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Keep looking so long as the comments are still Doxygen comments and are
3902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // still adjacent.
3912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned ExpectedLine
3922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
3932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator FirstComment = LastComment;
3942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != Comments.begin()) {
3952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Look at the previous comment
3962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    --FirstComment;
3972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> Decomp
3982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
3992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this previous comment is in a different file, we're done.
4012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Decomp.first != DeclStartDecomp.first) {
4022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this comment is not a Doxygen comment, we're done.
4072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!isDoxygenComment(SourceMgr, *FirstComment)) {
4082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If the line number is not what we expected, we're done.
4132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
4142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Line != ExpectedLine) {
4152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Set the next expected line number.
4202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ExpectedLine
4212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
4222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // The iterator range [FirstComment, LastComment] contains all of the
4252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // BCPL comments that, together, are associated with this declaration.
4262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Form a single comment block string for this declaration that concatenates
4272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // all of these comments.
4282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::string &Result = DeclComments[D];
4292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != LastComment) {
4302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompStart
4312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
4322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompEnd
4332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
4342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    Result.append(FileBufferStart + DecompStart.second,
4352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                  FileBufferStart + DecompEnd.second + 1);
4362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ++FirstComment;
4372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Append the last comment line.
4402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Result.append(FileBufferStart +
4412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                  SourceMgr.getFileOffset(LastComment->getBegin()),
4422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                FileBufferStart + LastCommentEndDecomp.second + 1);
4432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return Result.c_str();
4442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
4452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
446464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
447464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
448464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
449a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
450b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
451b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
452b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
453b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  const BuiltinType *BT = T->getAsBuiltinType();
454b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
455b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
456b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
457b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
458b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
459b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
460b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
461b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
462b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
463af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// getDeclAlign - Return a conservative estimate of the alignment of the
464af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
465af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
466b7d0844c887a72064b624dc6df12cbe1441f69d0Daniel Dunbarunsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
467dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
468dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
46940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
470dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    Align = std::max(Align, AA->getAlignment());
471dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
472af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
473af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
4746217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
4754cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      unsigned AS = RT->getPointeeType().getAddressSpace();
476f0930235ce58a91aa3b840bece9052f44d630536Anders Carlsson      Align = Target.getPointerAlign(AS);
4774cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson    } else if (!T->isIncompleteType() && !T->isFunctionType()) {
4784cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
479dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
480dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
481dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
482dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
483dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
484af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
485dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
486dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  return Align / Target.getCharWidth();
487af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
488b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
489a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
490a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
491d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
4921d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
4935e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
4945e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
495a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
49672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
49772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
49818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
49972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
50072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
50118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
50272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
50372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5045d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
5055d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
50618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
50718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
50818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
50918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
51018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
51172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
512fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
51318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
51418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
51518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
51618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5177e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithExpr:
5187e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithoutExpr:
519fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
5201d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
521030d8846c7e520330007087e949f621989876e3aChris Lattner
52298be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
5239e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
524030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
525030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
5265c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
527213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
528030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
529030d8846c7e520330007087e949f621989876e3aChris Lattner    std::pair<uint64_t, unsigned> EltInfo =
53098be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<VectorType>(T)->getElementType());
5319e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
5324bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
5336fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
5346fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
5356fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // FIXME: this should probably be a target property.
5366fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    Align = 1 << llvm::Log2_32_Ceil(Align);
537030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
538030d8846c7e520330007087e949f621989876e3aChris Lattner  }
5395d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
5409e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
541a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
542692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
543d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
54418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
54518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
54618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
54718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
54818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
5509e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
5519e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
5526f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
553692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
554692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
555692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
5566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
5579e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
5589e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
5596f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
56064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
56164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
56264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
56364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
564f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
565f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
566f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
567f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
568f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
569f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
570f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
571f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
572692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
5736f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
5749e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
5759e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
5766f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
577692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
5786f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
5799e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
5809e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
5816f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
582692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
5836f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
5849e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
5859e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
5866f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
587692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
5886f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
5899e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
5909e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
5916f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
592ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
593ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
594ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
595ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
596ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
5976f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
5989e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
5999e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
6006f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6016f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
6025426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
6035426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
6046f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6056f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
6069e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
6079e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
6086f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6096e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
6106e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
6116e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
6121590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
613a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
614bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
615f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  case Type::FixedWidthInt:
616f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // FIXME: This isn't precisely correct; the width/alignment should depend
617f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // on the available types for the target
618f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Width = cast<FixedWidthIntType>(T)->getWidth();
619736166b38235cf6d0ffb67638960d95fb2afcbd6Chris Lattner    Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
620f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Align = Width;
621f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    break;
622f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  case Type::ExtQual:
62398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // FIXME: Pointers into different addr spaces could have different sizes and
62498be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // alignment requirements: getPointerInfo should take an AddrSpace.
625f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
626d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
6275426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
628f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
6296f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
630485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
631485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
632485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
633485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
634485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
635485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
636f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
637f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
6385426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
639f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
640f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
641f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
6427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
6437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
6447ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner    // "When applied to a reference or a reference type, the result is the size
6455d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // of the referenced type." C++98 5.3.3p2: expr.sizeof.
6466f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // FIXME: This is wrong for struct layout: a reference in a struct has
6476f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // pointer size.
648bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
649f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
6501cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
6511cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
6521cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // If we ever want to support other ABIs this needs to be abstracted.
6531cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson
654f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
6551cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    std::pair<uint64_t, unsigned> PtrDiffInfo =
6561cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
6571cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
658f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
659f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
6601cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
6611cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
662f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
6635d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
6645d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
6655d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
6665d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    std::pair<uint64_t, unsigned> EltInfo =
66798be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
6689e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
6695d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
6705d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
6715d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
67244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
6731d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
67444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
67544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
67644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
67744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
67844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
67972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
68072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
6811d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
6821d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
6831d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
6848389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
6858389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
6868389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
6878389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
6888389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner
6891d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
6907176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
6917176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
6921d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
6937176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
6947176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
6957176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
696dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
697a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
6987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
69918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
70018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
70140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
70218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = Aligned->getAlignment();
70318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
70418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
70518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
7067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
7077176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
70818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
70918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
71018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
71118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
71218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
71318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
71418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
71518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
716395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
717395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
718395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
719395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
72018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::QualifiedName:
72118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
72218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
72318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
72418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(getCanonicalType(T) != T &&
72518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
72618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
72718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
72818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
72918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
73018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
731d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
732464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
7339e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
734a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
735a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
73634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
73734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
73834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
73934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
74034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
74134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
7421eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
7431eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
7441eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (const ComplexType* CT = T->getAsComplexType())
7451eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
7461eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
7471eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
7481eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
7491eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
75034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
75134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
75234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
753a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
754a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
755a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
756a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
757a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
758f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
759a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
760a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
761a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
762a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
763a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
764a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
765a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
766a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
767a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
768a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
769a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
770a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
7718e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
7728e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
7738e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
7748e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
7758e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
7768e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 bool CollectSynthesized) {
7778e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
7788e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian         E = OI->ivar_end(); I != E; ++I) {
7798e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian     Ivars.push_back(*I);
7808e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
7818e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  if (CollectSynthesized)
7828e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    CollectSynthesizedIvars(OI, Ivars);
7838e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
7848e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
7859820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
7869820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
78717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
78817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
7899820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
7909820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
7919820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
7929820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into nested protocols.
7939820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
7949820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
7959820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(*P, Ivars);
7969820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
7979820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
7989820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// CollectSynthesizedIvars -
7999820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// This routine collect synthesized ivars for the designated class.
8009820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
8019820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
8029820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
80317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
80417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8059820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
8069820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
8079820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8089820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into interface's protocol list for properties declared
8099820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // in the protocol and whose ivars are synthesized.
8109820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8119820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8129820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8139820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(PD, Ivars);
8149820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8159820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
8169820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
8178e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
8188e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
81917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
82017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
8218e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8228e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8238e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8248e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into nested protocols.
8258e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
8268e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
8278e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(*P);
8288e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8298e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8308e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8318e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI)
8328e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian{
8338e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
83417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
83517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8368e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8378e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8388e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8398e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into interface's protocol list for properties declared
8408e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // in the protocol and whose ivars are synthesized.
8418e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8428e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8438e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8448e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(PD);
8458e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8468e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8478e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8488e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8498a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
8508a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
8518a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8528a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8538a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
8558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
8588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
8598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
8638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8648a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8658a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
8668a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
8678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
8688a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
8698a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
8708a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
8718a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8728a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
8738a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
8748a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
8758a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
8768a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
8778a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8788a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
879b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Allocate an uninitialized DeclaratorInfo.
880b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
881b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// The caller should initialize the memory held by DeclaratorInfo using
882b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
883b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
884b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
885b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
886b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
887b17166c8077cd900cca83a895c43b30ea6660598Argyrios KyrtzidisDeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
888b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
889b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  DeclaratorInfo *DInfo =
890b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis    (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
891b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  new (DInfo) DeclaratorInfo(T);
892b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  return DInfo;
893b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
894b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
895b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the
896b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface.
897b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar///
898b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's
899b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars.
90044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout &
901b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
902b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar                          const ObjCImplementationDecl *Impl) {
903532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar  assert(!D->isForwardDecl() && "Invalid interface decl!");
904532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar
90544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Look up this layout, if already laid out, return what we have.
906d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  ObjCContainerDecl *Key =
907d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
908d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
909d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    return *Entry;
91044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
911453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // Add in synthesized ivar count if laying out an implementation.
912453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
91329445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    unsigned FieldCount = D->ivar_size();
9148e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    unsigned SynthCount = CountSynthesizedIvars(D);
9158e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    FieldCount += SynthCount;
916d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    // If there aren't any sythesized ivars then reuse the interface
917453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entry. Note we can't cache this because we simply free all
918453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entries later; however we shouldn't look up implementations
919453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // frequently.
9208e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if (SynthCount == 0)
921453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar      return getObjCLayout(D, 0);
922453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  }
923453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar
92429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  const ASTRecordLayout *NewEntry =
92529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
92629445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  ObjCLayouts[Key] = NewEntry;
92729445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
92844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  return *NewEntry;
92944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel}
93044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
931b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
932b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
933b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
934b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
935b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
936b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
937b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
938b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
939b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
940b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
94188a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
942464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
943464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
94498be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
9454b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  D = D->getDefinition(*this);
9464b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  assert(D && "Cannot get layout of forward declarations!");
9474bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
948464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
949ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // Note that we can't save a reference to the entry because this function
950ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // is recursive.
951ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  const ASTRecordLayout *Entry = ASTRecordLayouts[D];
952464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
9534bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
95429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  const ASTRecordLayout *NewEntry =
95529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D);
956ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  ASTRecordLayouts[D] = NewEntry;
95729445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
9585d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
959464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
960464175bba1318bef7905122e9fda20cff926df78Chris Lattner
961a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
962a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
963a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
964a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
965f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
966f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
967f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
968f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
969b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
970b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
971b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
972b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
973b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttr = QualType::GCNone;
974b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
975b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
976b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
977b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // If this type already has an address space specified, it cannot get
978b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
979b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getAddressSpace() == 0 &&
980b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner           "Type cannot be in multiple addr spaces!");
981b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttr = EQT->getObjCGCAttr();
982b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
983b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
984f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner
985b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // Check if we've already instantiated this type.
986ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  llvm::FoldingSetNodeID ID;
9872455636163fdd18581d7fdae816433f886d88213Mike Stump  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
988ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  void *InsertPos = 0;
989f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
990b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
991b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
992ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // If the base type isn't canonical, this won't be a canonical type either,
993ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // so fill in the canonical type field.
994ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  QualType Canonical;
995b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (!TypeNode->isCanonical()) {
996f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    Canonical = getAddrSpaceQualType(CanT, AddressSpace);
997ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
998b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
999f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1000f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1001ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  }
1002b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
1003b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
1004f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
1005ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  Types.push_back(New);
1006b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
1007ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1008ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1009b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
1010b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner                                       QualType::GCAttrTypes GCAttr) {
1011d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1012b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1013d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
1014d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
10154027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  if (T->isPointerType()) {
10166217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
101758f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
10184027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
10194027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
10204027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
10214027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
1022b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
1023b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
1024b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
1025b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
1026b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned AddressSpace = 0;
1027b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
1028b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
10292455636163fdd18581d7fdae816433f886d88213Mike Stump    // If this type already has an ObjCGC specified, it cannot get
1030b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
1031b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getObjCGCAttr() == QualType::GCNone &&
10322455636163fdd18581d7fdae816433f886d88213Mike Stump           "Type cannot have multiple ObjCGCs!");
1033b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    AddressSpace = EQT->getAddressSpace();
1034b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
1035b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
1036d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1037d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check if we've already instantiated an gc qual'd type of this type.
1038d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  llvm::FoldingSetNodeID ID;
10392455636163fdd18581d7fdae816433f886d88213Mike Stump  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
1040d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  void *InsertPos = 0;
1041d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
1042b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
1043d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1044d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // If the base type isn't canonical, this won't be a canonical type either,
1045d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // so fill in the canonical type field.
10465a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // FIXME: Isn't this also not canonical if the base type is a array
10475a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // or pointer type?  I can't find any documentation for objc_gc, though...
1048d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType Canonical;
1049d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (!T->isCanonical()) {
1050b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    Canonical = getObjCGCQualType(CanT, GCAttr);
1051d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1052b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
1053d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1054d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1055d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1056b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
1057b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
1058d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
1059d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  Types.push_back(New);
1060b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
1061d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1062a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
10632455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getNoReturnType(QualType T) {
10646dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  QualifierSet qs;
10656dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  qs.strip(T);
10662455636163fdd18581d7fdae816433f886d88213Mike Stump  if (T->isPointerType()) {
10676217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
10682455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType ResultType = getNoReturnType(Pointee);
10696dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getPointerType(ResultType);
10706dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType.setCVRQualifiers(T.getCVRQualifiers());
10716dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    return qs.apply(ResultType, *this);
10722455636163fdd18581d7fdae816433f886d88213Mike Stump  }
10732455636163fdd18581d7fdae816433f886d88213Mike Stump  if (T->isBlockPointerType()) {
10746217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
10752455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType ResultType = getNoReturnType(Pointee);
10766dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getBlockPointerType(ResultType);
10776dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType.setCVRQualifiers(T.getCVRQualifiers());
10786dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    return qs.apply(ResultType, *this);
10792455636163fdd18581d7fdae816433f886d88213Mike Stump  }
10802455636163fdd18581d7fdae816433f886d88213Mike Stump  if (!T->isFunctionType())
10812455636163fdd18581d7fdae816433f886d88213Mike Stump    assert(0 && "can't noreturn qualify non-pointer to function or block type");
10822455636163fdd18581d7fdae816433f886d88213Mike Stump
1083e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump  if (const FunctionNoProtoType *F = T->getAsFunctionNoProtoType()) {
10842455636163fdd18581d7fdae816433f886d88213Mike Stump    return getFunctionNoProtoType(F->getResultType(), true);
10852455636163fdd18581d7fdae816433f886d88213Mike Stump  }
1086e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump  const FunctionProtoType *F = T->getAsFunctionProtoType();
10872455636163fdd18581d7fdae816433f886d88213Mike Stump  return getFunctionType(F->getResultType(), F->arg_type_begin(),
10882455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
10892455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
10902455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->getNumExceptions(), F->exception_begin(), true);
10912455636163fdd18581d7fdae816433f886d88213Mike Stump}
10922455636163fdd18581d7fdae816433f886d88213Mike Stump
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1109f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1113f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1115f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ComplexType *New = new (*this,8) ComplexType(T, Canonical);
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1121f98aba35e6c3da5aae61843fc01334939e4e12ecEli FriedmanQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1122f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1123f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman     SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1124f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  FixedWidthIntType *&Entry = Map[Width];
1125f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (!Entry)
1126f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Entry = new FixedWidthIntType(Width, Signed);
1127f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  return QualType(Entry, 0);
1128f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman}
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1146f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1150f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1152f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  PointerType *New = new (*this,8) PointerType(T, Canonical);
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11585618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// getBlockPointerType - Return the uniqued reference to the type for
11595618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
11605618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
1161296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1162296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
11635618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
11645618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
11655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
11665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11675618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
11685618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
11695618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11705618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
11715618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
1172296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // If the block pointee type isn't canonical, this won't be a canonical
11735618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
11745618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
11755618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (!T->isCanonical()) {
11765618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
11775618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11785618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
11795618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
11805618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1181f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11825618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
1183f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
11845618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
11855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
11865618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
11875618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
11885618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
11907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
11917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getLValueReferenceType(QualType T) {
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceType::Profile(ID, T);
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
11997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
12017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
12045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
12067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getLValueReferenceType(getCanonicalType(T));
12077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
12097c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
12107c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
12117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
12137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
12157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
12167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
12177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
12187c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
12197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12207c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
12217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
12227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
12237c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
12247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
12257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
12267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  ReferenceType::Profile(ID, T);
12277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
12297c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
12307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
12327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12337c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
12347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
12357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
12367c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (!T->isCanonical()) {
12377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getRValueReferenceType(getCanonicalType(T));
12387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
12417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1242f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1251f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1252f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
1253f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian RedlQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1254f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl{
1255f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1256f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1257f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1258f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1259f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1260f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1261f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1262f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1263f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1264f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1265f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1266f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1267f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
1268f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (!T->isCanonical()) {
1269f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1270f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1271f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1272f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1273f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1274f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1275f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
1276f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
1277f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1278f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1279f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1280f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1281f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1282fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// getConstantArrayType - Return the unique reference to the type for an
1283fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
1284fb22d96692c5240fb8d611290dbf7eeed3759c73Steve NaroffQualType ASTContext::getConstantArrayType(QualType EltTy,
128538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1286c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1287c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1288587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman  assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1289587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1290587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
129138aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
129238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
129338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
129438aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
129538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner
12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
12970be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13007192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  if (ConstantArrayType *ATP =
13017192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
13025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
13055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
13075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!EltTy->isCanonical()) {
1308f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1309c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
13117192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek    ConstantArrayType *NewIP =
13127192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1313f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
13145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1316566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  ConstantArrayType *New =
1317f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
13187192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
13195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13237e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithExprType - Return a reference to the type for
13247e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13257e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13267e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithExprType(QualType EltTy,
13277e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         const llvm::APInt &ArySizeIn,
13287e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         Expr *ArySizeExpr,
13297e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         ArrayType::ArraySizeModifier ASM,
13307e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         unsigned EltTypeQuals,
13317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         SourceRange Brackets) {
13327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13347e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13357e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13377e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
13387e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
13397e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
13407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Since we don't unique expressions, it isn't possible to unique VLA's
13417e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // that have an expression provided for their size.
13427e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithExprType *New =
13437e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
13447e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ArySize, ArySizeExpr,
13457e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ASM, EltTypeQuals, Brackets);
13467e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
13477e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
13487e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
13497e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13507e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithoutExprType - Return a reference to the type for
13517e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13527e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13537e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithoutExprType(QualType EltTy,
13547e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            const llvm::APInt &ArySizeIn,
13557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArrayType::ArraySizeModifier ASM,
13567e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            unsigned EltTypeQuals) {
13577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13587e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13597e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13617e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
13637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
13647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
13657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithoutExprType *New =
13667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
13677e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                             ArySize, ASM, EltTypeQuals);
13687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
13697e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
13707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
13717e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
1372bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1373bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
13747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
13757e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1376c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
13777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          unsigned EltTypeQuals,
13787e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          SourceRange Brackets) {
1379c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1380c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1381c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1382566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  VariableArrayType *New =
13837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)VariableArrayType(EltTy, QualType(),
13847e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  NumElts, ASM, EltTypeQuals, Brackets);
1385c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1386c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1387c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1388c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1389c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1390c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1391898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1392898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
139304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
13947e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy,
13957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                Expr *NumElts,
1396898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
13977e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                unsigned EltTypeQuals,
13987e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                SourceRange Brackets) {
1399898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1400898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1401898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
140204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  llvm::FoldingSetNodeID ID;
140304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
140404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                   EltTypeQuals, NumElts);
1405898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
140604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  void *InsertPos = 0;
140704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *Canon
140804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
140904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *New;
141004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  if (Canon) {
141104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // We already have a canonical version of this array type; use it as
141204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // the canonical type for a newly-built type.
141304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    New = new (*this,8) DependentSizedArrayType(*this, EltTy,
141404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                QualType(Canon, 0),
141504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                NumElts, ASM, EltTypeQuals,
141604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                Brackets);
141704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  } else {
141804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    QualType CanonEltTy = getCanonicalType(EltTy);
141904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    if (CanonEltTy == EltTy) {
142004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      New = new (*this,8) DependentSizedArrayType(*this, EltTy, QualType(),
142104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  NumElts, ASM, EltTypeQuals,
142204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  Brackets);
142304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      DependentSizedArrayTypes.InsertNode(New, InsertPos);
142404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    } else {
142504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
142604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  ASM, EltTypeQuals,
142704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  SourceRange());
142804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      New = new (*this,8) DependentSizedArrayType(*this, EltTy, Canon,
142904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  NumElts, ASM, EltTypeQuals,
143004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  Brackets);
143104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    }
143204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  }
143304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor
1434898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1435898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1436898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1437898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1438c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1439c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1440c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1441c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
14420be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1443c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1444c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
1445c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (IncompleteArrayType *ATP =
1446c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1447c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1448c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1449c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1450c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1451c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1452c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1453c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (!EltTy->isCanonical()) {
1454f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
14552bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1456c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1457c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1458c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1459c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1460f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14612bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1462c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
14637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  IncompleteArrayType *New
14647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    = new (*this,8) IncompleteArrayType(EltTy, Canonical,
14657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                        ASM, EltTypeQuals);
1466c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1467c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1468c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1469c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1470fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1471fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
147273322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
147373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
147473322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1477f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
147873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
148273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorType::Profile(ID, vecType, NumElts, Type::Vector);
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
14855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!vecType->isCanonical()) {
1491f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getVectorType(getCanonicalType(vecType), NumElts);
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1495f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1497f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1503213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
150473322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1505213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
150673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
150773322924127c873c13101b705dd823f5539ffa5fSteve Naroff
1508f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1509213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
151073322924127c873c13101b705dd823f5539ffa5fSteve Naroff
151173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
151273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
1513213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
151473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
151573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
151673322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
151773322924127c873c13101b705dd823f5539ffa5fSteve Naroff
151873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
151973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
152073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
152173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (!vecType->isCanonical()) {
1522213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
152373322924127c873c13101b705dd823f5539ffa5fSteve Naroff
152473322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
152573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1526f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
152773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
1528f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
152973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
153073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
153173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
153273322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
153373322924127c873c13101b705dd823f5539ffa5fSteve Naroff
15349cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas GregorQualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
15359cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    Expr *SizeExpr,
15369cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    SourceLocation AttrLoc) {
15372ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
15382ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
15392ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
15402ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor
15412ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
15422ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
15432ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
15442ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
15452ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
15462ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
15472ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
15482ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
15492ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                    QualType(Canon, 0),
15502ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                    SizeExpr, AttrLoc);
15512ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
15522ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
15532ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
15542ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
15552ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      QualType(), SizeExpr,
15562ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      AttrLoc);
15572ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
15582ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
15592ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
15602ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
15612ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      New = new (*this,8) DependentSizedExtVectorType(*this, vecType, Canon,
15622ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SizeExpr, AttrLoc);
15632ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
15642ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
15652ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor
15669cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
15679cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
15689cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15699cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
157072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
15715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
15722455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
15735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
15762455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
157972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (FunctionNoProtoType *FT =
158072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
15825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
15845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!ResultTy->isCanonical()) {
15852455636163fdd18581d7fdae816433f886d88213Mike Stump    Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
158872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
158972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1590f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15932455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType *New
15942455636163fdd18581d7fdae816433f886d88213Mike Stump    = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn);
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
159672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
160261710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1603971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1604465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     unsigned TypeQuals, bool hasExceptionSpec,
1605465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     bool hasAnyExceptionSpec, unsigned NumExs,
16062455636163fdd18581d7fdae816433f886d88213Mike Stump                                     const QualType *ExArray, bool NoReturn) {
16075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
161072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1611465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                             TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
16122455636163fdd18581d7fdae816433f886d88213Mike Stump                             NumExs, ExArray, NoReturn);
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
161572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (FunctionProtoType *FTP =
161672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1618465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1619465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCanonical = ResultTy->isCanonical();
1621465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  if (hasExceptionSpec)
1622465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    isCanonical = false;
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!ArgArray[i]->isCanonical())
16255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
16265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
1628465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
16305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isCanonical) {
16315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
1634f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner      CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
1635465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1636f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1637beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
163847259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                isVariadic, TypeQuals, false,
163947259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                false, 0, 0, NoReturn);
1640465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
164272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
164372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1644f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1646465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
164772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1648465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
1649465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // end of them.
165072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType *FTP =
1651465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1652465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumArgs*sizeof(QualType) +
1653465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumExs*sizeof(QualType), 8);
165472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1655465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                              TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
16562455636163fdd18581d7fdae816433f886d88213Mike Stump                              ExArray, NumExs, Canonical, NoReturn);
16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
165872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16622ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
16632ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
16644b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
16651e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
16662ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
16672ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
16681e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
16692ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1670fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  else if (isa<TemplateTypeParmDecl>(Decl)) {
1671fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    assert(false && "Template type parameter types are always available.");
16729fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else if (ObjCInterfaceDecl *ObjCInterface
16739fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump               = dyn_cast<ObjCInterfaceDecl>(Decl))
16742ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getObjCInterfaceType(ObjCInterface);
167549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1676c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1677566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1678566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1679f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1680f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) RecordType(Record);
16819fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1682566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1683566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1684f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1685f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) EnumType(Enum);
16869fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
16872ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    assert(false && "TypeDecl without a type?");
168849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
16894b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
169049aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
16912ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
16922ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
16935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
16945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
16955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) {
16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
16975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1698f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
169972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
17015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
17025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1704fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
170576e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// parameter or parameter pack with the given depth, index, and (optionally)
170676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
1707fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas GregorQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
170876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
1709fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1710fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
171176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
1712fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
1713fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmType *TypeParm
1714fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1715fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1716fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1717fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
1718fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
171976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  if (Name) {
172076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
172176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
172276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                                   Name, Canon);
172376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
172476e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
1725fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1726fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1727fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1728fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1729fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1730fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1731fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
173255f6b14230c94272efbbcdd89a92224c8db9f225Douglas GregorQualType
17337532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
17347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
17357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
17367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          QualType Canon) {
1737b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  if (!Canon.isNull())
1738b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    Canon = getCanonicalType(Canon);
1739b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  else {
1740b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    // Build the canonical template specialization type.
17411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateName CanonTemplate = getCanonicalTemplateName(Template);
17421275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::SmallVector<TemplateArgument, 4> CanonArgs;
17431275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    CanonArgs.reserve(NumArgs);
17441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
17451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
17461275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
17471275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // Determine whether this canonical template specialization type already
17481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // exists.
17491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::FoldingSetNodeID ID;
17501275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateSpecializationType::Profile(ID, CanonTemplate,
1751828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                        CanonArgs.data(), NumArgs, *this);
17521275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
17531275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    void *InsertPos = 0;
17541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateSpecializationType *Spec
17551275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
17561275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
17571275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    if (!Spec) {
17581275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // Allocate a new canonical template specialization type.
17591275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      void *Mem = Allocate((sizeof(TemplateSpecializationType) +
17601275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                            sizeof(TemplateArgument) * NumArgs),
17611275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                           8);
1762828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor      Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
17631275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                                  CanonArgs.data(), NumArgs,
1764b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                                                  Canon);
17651275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Types.push_back(Spec);
17661275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
17671275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
17681275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
1769b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    if (Canon.isNull())
1770b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor      Canon = QualType(Spec, 0);
17711275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    assert(Canon->isDependentType() &&
17721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           "Non-dependent template-id type must have a canonical type");
1773b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  }
1774fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
17751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
17761275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
17771275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
17787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
177940808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
178040808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                       8);
17811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  TemplateSpecializationType *Spec
1782828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor    = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
1783828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                           Canon);
17841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
178555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
178655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  return QualType(Spec, 0);
178755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
178855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
1789e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas GregorQualType
1790ab452ba8323d1985e08bade2bced588cddf2cc28Douglas GregorASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
1791e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor                                 QualType NamedType) {
1792e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1793ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  QualifiedNameType::Profile(ID, NNS, NamedType);
1794e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1795e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
1796e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameType *T
1797e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1798e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1799e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1800e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1801ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  T = new (*this) QualifiedNameType(NNS, NamedType,
1802ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor                                    getCanonicalType(NamedType));
1803e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1804e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameTypes.InsertNode(T, InsertPos);
1805e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1806e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1807e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1808d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1809d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     const IdentifierInfo *Name,
1810d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     QualType Canon) {
1811d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1812d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1813d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1814d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1815d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (CanonNNS != NNS)
1816d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Canon = getTypenameType(CanonNNS, Name);
1817d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1818d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1819d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
1820d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType::Profile(ID, NNS, Name);
1821d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1822d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
1823d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType *T
1824d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1825d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1826d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1827d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1828d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  T = new (*this) TypenameType(NNS, Name, Canon);
1829d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
1830d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
1831d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return QualType(T, 0);
1832d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1833d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
18341734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorQualType
18351734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorASTContext::getTypenameType(NestedNameSpecifier *NNS,
18361734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            const TemplateSpecializationType *TemplateId,
18371734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            QualType Canon) {
18381734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
18391734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18401734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
18411734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
18421734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
18431734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
18441734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
18451734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        = CanonType->getAsTemplateSpecializationType();
18461734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
18471734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
18481734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Canon = getTypenameType(CanonNNS, CanonTemplateId);
18491734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
18501734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
18511734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18521734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  llvm::FoldingSetNodeID ID;
18531734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType::Profile(ID, NNS, TemplateId);
18541734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18551734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  void *InsertPos = 0;
18561734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType *T
18571734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
18581734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (T)
18591734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    return QualType(T, 0);
18601734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18611734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  T = new (*this) TypenameType(NNS, TemplateId, Canon);
18621734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
18631734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
18641734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  return QualType(T, 0);
18651734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
18661734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
186788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
186888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
186988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
187088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
18712e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
187288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
187388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
187488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
187588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
187688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
187788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
187888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
187988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
188088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
188188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
188288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
188388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
188488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
188588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1886d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1887d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// the given interface decl and the conforming protocol list.
188814108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
1889d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff                                              ObjCProtocolDecl **Protocols,
1890d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff                                              unsigned NumProtocols) {
1891d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // Sort the protocol list alphabetically to canonicalize it.
1892d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (NumProtocols)
1893d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
1894d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1895d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  llvm::FoldingSetNodeID ID;
189614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
1897d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1898d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
1899d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (ObjCObjectPointerType *QT =
1900d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1901d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    return QualType(QT, 0);
1902d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1903d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // No Match;
1904d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerType *QType =
190514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
1906d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1907d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  Types.push_back(QType);
1908d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1909d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  return QualType(QType, 0);
1910d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
191188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1912c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// getObjCInterfaceType - Return the unique reference to the type for the
1913c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// specified ObjC interface decl. The list of protocols is optional.
1914c15cb2af27514ecc879daba9aa01389c5203685dSteve NaroffQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
1915a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                       ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
1916c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (NumProtocols)
1917c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    // Sort the protocol list alphabetically to canonicalize it.
1918c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
191988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
19204b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
1921c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
19224b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
19234b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
1924c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (ObjCInterfaceType *QT =
1925c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff      ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
19264b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
19274b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
19284b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  // No Match;
1929c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType *QType =
1930c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1931c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                    Protocols, NumProtocols);
19324b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  Types.push_back(QType);
1933c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceTypes.InsertNode(QType, InsertPos);
19344b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  return QualType(QType, 0);
19354b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian}
19364b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
193772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
193872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
19399752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
19409752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// DeclRefExpr's. This doesn't effect the type checker, since it operates
19419752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
194272564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
1943dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
1944b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
1945b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
1946b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
1947b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor
1948b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
1949b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
1950b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
1951b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
1952b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
1953b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
1954b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = new (*this, 8) TypeOfExprType(tofExpr,
1955b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
1956b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
1957b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
1958b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
1959b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr);
1960b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
1961b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
1962b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
1963b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
1964dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
1965dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
1966dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
19679752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
19689752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
1969d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
1970d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
19719752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
19729752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
19739752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
19749752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// an issue. This doesn't effect the type checker, since it operates
19759752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
1976d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
1977f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
1978f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
19799752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
19809752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
1981d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
1982d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
198360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
198460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
198560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
1986a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
1987a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
1988a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson
198960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
199060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
199160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
199260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
199360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
199460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
199560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
199660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
199760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
199860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
199960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
200060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
200160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
200260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
200360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
200460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
200560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
200660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
200760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
200860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
200960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (e->isLvalue(Context) == Expr::LV_Valid)
201060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
201160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
201260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
201360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
201460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2015395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2016395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2017395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
2018395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// an issue. This doesn't effect the type checker, since it operates
2019395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
2020395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) {
2021dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
20229d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  if (e->isTypeDependent()) {
20239d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
20249d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
20259d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor
20269d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
20279d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
20289d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
20299d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
20309d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
20319d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
20329d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = new (*this, 8) DecltypeType(e, DependentTy,
20339d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
20349d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
20359d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
20369d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
20379d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      Canon = new (*this, 8) DependentDecltypeType(*this, e);
20389d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
20399d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
20409d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
20419d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2042dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
2043563a03b1338d31c2462def43253a722bc885d384Anders Carlsson    dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
2044dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2045395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2046395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2047395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2048395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
20495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
2051e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) {
2052d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2053e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2054e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2055e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
20565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
20595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
20605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// needs to agree with the definition in <stddef.h>.
20615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const {
2062b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
206564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
206664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
206764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
206864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
206964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
207064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
207164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
207264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
207364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
207464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
207564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
207664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
207764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
207864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
20798b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
20808b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
20818b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2082b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
20838b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
20848b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2085e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2086e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2087e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2088e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
208977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
209077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
209177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
209277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
209377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
209477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
209550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) {
209677c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner  QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
2097c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2098c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the result has type qualifiers, make sure to canonicalize them as well.
2099c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
210050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  if (TypeQuals == 0)
210150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType);
2102c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2103c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the type qualifiers are on an array type, get the canonical type of the
2104c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // array with the qualifiers applied to the element type.
2105c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
2106c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
210750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType.getQualifiedType(TypeQuals));
2108c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2109c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2110c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
2111c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
2112c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
2113c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2114c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
211550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
211650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getConstantArrayType(NewEltTy, CAT->getSize(),
211750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getSizeModifier(),
211850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getIndexTypeQualifier()));
2119c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
212050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
212150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
212250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                    IAT->getIndexTypeQualifier()));
2123c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2124898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
212550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
212650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getDependentSizedArrayType(NewEltTy,
2127bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                        DSAT->getSizeExpr() ?
2128bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                          DSAT->getSizeExpr()->Retain() : 0,
212950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getSizeModifier(),
213050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getIndexTypeQualifier(),
213150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getBracketsRange()));
2132898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2133c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
213450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
2135bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                        VAT->getSizeExpr() ?
2136bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr()->Retain() : 0,
213750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                        VAT->getSizeModifier(),
213850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                  VAT->getIndexTypeQualifier(),
213950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                     VAT->getBracketsRange()));
2140c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
2141c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
214225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
214325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
214425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
214525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
214697fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
214725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
2148d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  // If this template name refers to a set of overloaded function templates,
2149d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  /// the canonical template name merely stores the set of function templates.
21506ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor  if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
21516ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    OverloadedFunctionDecl *CanonOvl = 0;
21526ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
21536ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                                                FEnd = Ovl->function_end();
21546ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor         F != FEnd; ++F) {
21556ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      Decl *Canon = F->get()->getCanonicalDecl();
21566ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      if (CanonOvl || Canon != F->get()) {
21576ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor        if (!CanonOvl)
21586ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor          CanonOvl = OverloadedFunctionDecl::Create(*this,
21596ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                                                    Ovl->getDeclContext(),
21606ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                                                    Ovl->getDeclName());
21616ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor
21626ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor        CanonOvl->addOverload(
21636ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                    AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
21646ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      }
21656ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    }
21666ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor
21676ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    return TemplateName(CanonOvl? CanonOvl : Ovl);
21686ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor  }
2169d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
217025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
217125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
217225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
217325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
217425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
21751275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorTemplateArgument
21761275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
21771275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
21781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
21791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
21801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
21811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
21821275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Build canonical expression?
21831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
21841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
21851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
21861275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
21871275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              Arg.getAsDecl()->getCanonicalDecl());
21881275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
21891275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
21901275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
21911275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              *Arg.getAsIntegral(),
21921275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
21931275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
21941275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
21951275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
21961275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getAsType()));
21971275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
21981275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
21991275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Allocate in ASTContext
22001275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
22011275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
22021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
22031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
22041275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
22051275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
22061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
22071275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument Result;
22081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
22091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Result;
22101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
22111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
22121275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
22131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
22141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
22151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
22161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
22171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
2218d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
2219d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
2220d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (!NNS)
2221d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
2222d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2223d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
2224d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
2225d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
2226d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this,
2227d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2228d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
2229d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2230d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
2231d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
2232d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
2233d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2234d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2235d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
2236d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
2237d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
2238d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *Prefix = 0;
2239d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2240d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // FIXME: This isn't the right check!
2241d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (T->isDependentType())
2242d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
2243d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2244d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, Prefix,
2245d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2246d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
2247d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2248d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2249d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
2250d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
2251d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
2252d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2253d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2254d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
2255d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
2256d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2257d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2258c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2259c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
2260c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
2261c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (T.getCVRQualifiers() == 0) {
2262c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
2263c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2264c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
2265c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
2266c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2267c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the common negative case fast, ignoring CVR qualifiers.
2268c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
2269c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2270f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Make sure to look through type qualifiers (like ExtQuals) for the negative
2271c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // test.
2272c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!isa<ArrayType>(CType) &&
2273c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      !isa<ArrayType>(CType.getUnqualifiedType()))
2274c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
2275c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2276c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Apply any CVR qualifiers from the array type to the element type.  This
2277c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
2278c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
2279c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2280c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
2281c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
228250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
2283c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
2284c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned AddrSpace = 0;
2285c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  Type *Ty = T.getTypePtr();
2286c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2287f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Rip through ExtQualType's and typedefs to get to a concrete type.
2288c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  while (1) {
2289f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
2290f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      AddrSpace = EXTQT->getAddressSpace();
2291f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      Ty = EXTQT->getBaseType();
2292c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    } else {
2293c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      T = Ty->getDesugaredType();
2294c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
2295c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner        break;
2296c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      CVRQuals |= T.getCVRQualifiers();
2297c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      Ty = T.getTypePtr();
2298c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    }
2299c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
2300c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2301c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
2302c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2303c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
2304c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
2305c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2306c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
2307c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
2308c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2309c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
2310c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy = ATy->getElementType();
2311c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (AddrSpace)
2312f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
2313c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
2314c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2315c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2316c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2317c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
2318c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getIndexTypeQualifier()));
2319c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2320c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2321c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
23227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                  IAT->getIndexTypeQualifier()));
2323898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2324898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (const DependentSizedArrayType *DSAT
2325898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
2326898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
2327898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                     getDependentSizedArrayType(NewEltTy,
2328bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                DSAT->getSizeExpr() ?
2329bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              DSAT->getSizeExpr()->Retain() : 0,
2330898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
23317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getIndexTypeQualifier(),
23327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
2333c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2334c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
23357e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
2336bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr() ?
2337bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                               VAT->getSizeExpr()->Retain() : 0,
2338c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
23397e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getIndexTypeQualifier(),
23407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
234177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
234277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
234377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
2344e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
2345e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
2346e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
2347e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
2348e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
2349e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2350e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
2351c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
2352c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
2353c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
2354c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
2355c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2356c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
2357e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2358c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
2359e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2360e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
2361c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
2362e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
2363e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
23645e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) {
23655e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  QualifierSet qualifiers;
23665e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  while (true) {
23675e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    const Type *UT = qualifiers.strip(QT);
23685e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
23695e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor      QT = AT->getElementType();
23706dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    } else {
23715e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor      return qualifiers.apply(QT, *this);
23725e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    }
23735e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  }
23745e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
23755e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
2376d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel DunbarQualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
23776183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  QualType ElemTy = VAT->getElementType();
23786183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
23796183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
23806183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson    return getBaseElementType(VAT);
23816183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
23826183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
23836183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
23846183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
23850de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
23860de78998e7bda473b408437053e48661b510d453Fariborz Jahanianuint64_t
23870de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
23880de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
23890de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
23900de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
23910de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
23920de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
23930de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
23940de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
23950de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
23965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
23975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
2398a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
2399ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  if (const ComplexType *CT = T->getAsComplexType())
24005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
2401a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2402d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel Dunbar  assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
2403ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  switch (T->getAsBuiltinType()->getKind()) {
2404a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
24055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
24065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
24075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
24085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
24095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2411716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2412716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// point or a complex type (based on typeDomain/typeSize).
2413716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
2414716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
24151361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
24161361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
24171361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
24181361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
24191361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
2420716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
2421f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
2422f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
2423f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
2424f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
2425f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
24261361b11066239ea15764a2a844405352d87296b3Chris Lattner
24271361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
24281361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
24291361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
24301361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
24311361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
24321361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
24335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
24345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
24377cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
24387cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
24397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
2440a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2441a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
2442a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
2443a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2444a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
2445fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
2446a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
2447fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
2448fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
24495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2451f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2452f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
2453f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
2454f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
2455f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  assert(T->isCanonical() && "T should be canonicalized");
2456f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
2457f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    T = ET->getDecl()->getIntegerType().getTypePtr();
2458f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2459a34267595534a72703290153a6f7e3da1adcec59Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::WChar))
2460a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
2461a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
2462f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
2463f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2464f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2465f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
2466f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2467f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2468f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // There are two things which impact the integer rank: the width, and
2469f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // the ordering of builtins.  The builtin ordering is encoded in the
2470f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // bottom three bits; the width is encoded in the bits above that.
24711b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
2472f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth() << 3;
2473f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2474f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
24757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
24767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
2477f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
24787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
24797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
24807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
24817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
2482f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
24837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
24847cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
2485f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
24867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
24877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
2488f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
24897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
24907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
2491f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
24927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
24937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
2494f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
24952df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
24962df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
24972df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
2498f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
2499f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
2500f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
250104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
250204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
250304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
250404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
250504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
250604e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) {
250704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
250804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
250904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
251004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
251104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
251204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
251304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
251404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
251504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
251604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
251704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
251804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
251904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
252004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
252104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
252204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
252304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
252404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
252504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
252604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
252704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
252804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
252904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
253004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
253104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
2532a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
2533a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2534a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
2535a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2536a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
2537a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
2538a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
2539a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
2540a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
2541a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
2542a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2543a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2544a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
2545a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
25467cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getIntegerTypeOrder - Returns the highest ranked integer type:
25477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
25487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
25497cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
2550f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
2551f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
25527cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
25535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2554f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2555f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
25565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
25587cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
25595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
25617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
25627cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
25637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
25647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
25657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
25667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
25677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
25687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
25697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
25707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
25717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
25727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
25737cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // powers of two larger than each other, this is always safe.
25747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
25757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
25767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
25777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
25787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
25797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
25805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
25827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
25837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // powers of two larger than each other, this is always safe.
25847cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
25855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
258671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
258771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson// getCFConstantStringType - Return the type used for constant CFStrings.
258871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
258971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
25906c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner    CFConstantStringTypeDecl =
259139ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2592df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek                         &Idents.get("NSConstantString"));
2593f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
259471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
259571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
259671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
2597f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2598f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
259971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
2600f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
260171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
2602f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[3] = LongTy;
260371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
260444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
260544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
260644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
260744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
2608a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           FieldTypes[i], /*DInfo=*/0,
2609a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           /*BitWidth=*/0,
26104afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
261117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
261244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
261344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
261444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    CFConstantStringTypeDecl->completeDefinition(*this);
261571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
261671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
261771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
26188467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2619b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2620319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
26216217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2622319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2623319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2624319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2625319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2626bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders CarlssonQualType ASTContext::getObjCFastEnumerationStateType()
2627bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson{
2628bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
262944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
263044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
263144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                         &Idents.get("__objcFastEnumerationState"));
263244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
2633bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2634bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2635de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
2636bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2637bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2638bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2639bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
2640bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
264144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
264244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      FieldDecl *Field = FieldDecl::Create(*this,
264344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           ObjCFastEnumerationStateTypeDecl,
264444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
2645a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           FieldTypes[i], /*DInfo=*/0,
2646a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           /*BitWidth=*/0,
26474afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
264817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
264944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
2650bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
265144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
2652bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
2653bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2654bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2655bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2656bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2657319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
26586217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2659319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2660319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2661319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2662319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2663e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
2664e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
26652d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
2666e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
2667bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2668bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
266985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
267085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
267185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
267285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2673a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
267433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
2675a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) {
267698be4943e8dc4f3905629a7102668960873cf863Chris Lattner  uint64_t sz = getTypeSize(type);
267733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
267833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
267933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (sz > 0 && type->isIntegralType())
268098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = std::max(sz, getTypeSize(IntTy));
268133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
268233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
268398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = getTypeSize(VoidPtrTy);
268498be4943e8dc4f3905629a7102668960873cf863Chris Lattner  return sz / getTypeSize(CharTy);
268533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
268633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2687a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
268833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
2689c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
2690e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
2691c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2692ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
2693a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
269433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
26950d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
269633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
269733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
269833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
269933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
270098be4943e8dc4f3905629a7102668960873cf863Chris Lattner  int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
270133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
270233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
270333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  int ParmOffset = 2 * PtrSize;
270489951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
270589951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
270689951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
270789951a86b594513c2a013532ed45d197413b1087Chris Lattner    int sz = getObjCEncodingTypeSize(PType);
2708a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
270933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
271033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
271133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(ParmOffset);
271233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
271333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(PtrSize);
271433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
271533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
271633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
271789951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
271889951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
271989951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
27204306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    QualType PType = PVDecl->getOriginalType();
27214306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
2722ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2723ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
2724ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
2725bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
2726ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
2727ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
2728ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
2729ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
273033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
27314306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
27320d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
273333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    S += llvm::utostr(ParmOffset);
2734a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ParmOffset += getObjCEncodingTypeSize(PType);
273533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
273633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
273733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2738c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
273983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
2740c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2741c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
274283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// Property attributes are stored as a comma-delimited C string. The simple
274383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// attributes readonly and bycopy are encoded as single characters. The
274483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// parametrized attributes, getter=name, setter=name, and ivar=name, are
274583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// encoded as single characters, followed by an identifier. Property types
274683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// are also encoded as a parametrized attribute. The characters used to encode
274783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
274883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
274983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
275083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
275183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
275283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
275383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
275483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
275583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
275683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
275783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
275883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
275983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
276083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
276183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
276283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
2763c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2764c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
2765e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
2766c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
2767c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
2768c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
2769c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2770c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
2771c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
2772c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    if (const ObjCCategoryImplDecl *CID =
2773c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
2774c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
277517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
2776653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2777c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2778c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2779c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2780c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2781c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2782c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2783c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2784c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2785c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2786c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
278761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
2788c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
278917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
2790653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2791c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2792c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2793c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2794c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2795c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2796c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2797c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2798c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2799c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2800c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2801c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2802c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2803c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2804c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
2805c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2806c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
2807090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
2808090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
2809153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
2810090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
2811090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
2812c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2813c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
2814c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
2815c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
2816c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
2817c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
2818c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
2819c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Retain: S += ",&"; break;
2820c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2821c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2822c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2823c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
2824c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
2825c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
2826c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
2827c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2828090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2829090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
2830090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian
2831c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2832c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
2833077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
2834c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2835c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2836c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2837c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
2838077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
2839c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2840c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2841c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
2842c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2843c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
284439f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
2845c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2846c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2847c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
2848c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
2849c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2850a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
2851a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// Another legacy compatibility encoding: 32-bit longs are encoded as
2852c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian/// 'l' or 'L' , but not always.  For typedefs, we need to use
2853a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2854a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
2855a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
28568e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
2857a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
2858c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
2859c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2860a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
2861c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      else
2862c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
2863c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2864a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
2865a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2866a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
2867a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
2868a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
28697d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
2870153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
287182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
287282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
287382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
287482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
28755b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian  getObjCEncodingForTypeImpl(T, S, true, true, Field,
28765b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
287782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
287882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
28798b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanianstatic void EncodeBitField(const ASTContext *Context, std::string& S,
2880153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                           const FieldDecl *FD) {
28818b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
28828b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
28838b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
28849a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
28858b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
28868b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
28878b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
28888b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
288982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
289082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
289182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
2892153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
2893090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
28946ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
2895e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const BuiltinType *BT = T->getAsBuiltinType()) {
2896ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
2897ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      return EncodeBitField(this, S, FD);
2898ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    char encoding;
2899ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    switch (BT->getKind()) {
2900ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    default: assert(0 && "Unhandled builtin type kind");
2901ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Void:       encoding = 'v'; break;
2902ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Bool:       encoding = 'B'; break;
2903ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_U:
2904ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UChar:      encoding = 'C'; break;
2905ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UShort:     encoding = 'S'; break;
2906ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt:       encoding = 'I'; break;
2907ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULong:
290872696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        encoding =
2909ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner          (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
291072696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        break;
2911ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt128:    encoding = 'T'; break;
2912ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULongLong:  encoding = 'Q'; break;
2913ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_S:
2914ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::SChar:      encoding = 'c'; break;
2915ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Short:      encoding = 's'; break;
2916ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int:        encoding = 'i'; break;
2917ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Long:
2918ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      encoding =
2919ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2920ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      break;
2921ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongLong:   encoding = 'q'; break;
2922ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int128:     encoding = 't'; break;
2923ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Float:      encoding = 'f'; break;
2924ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Double:     encoding = 'd'; break;
2925ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongDouble: encoding = 'd'; break;
292643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
2927ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2928ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += encoding;
2929ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2930ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2931ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2932ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ComplexType *CT = T->getAsComplexType()) {
2933c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
2934c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2935c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
2936ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2937ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2938ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
29396217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
294085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    QualType PointeeTy = PT->getPointeeType();
2941a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
2942a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
2943a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
2944a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2945a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // Also, do not emit the 'r' for anything but the outermost type!
29468e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
2947a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
2948a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2949a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2950a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
29519fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
2952a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
29536217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
29546217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
2955a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
2956a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2957a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2958a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2959a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2960a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
2961a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
2962a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
2963a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
2964a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      const char * s = S.c_str();
2965a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      int len = S.length();
2966a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2967a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        std::string replace = "rn";
2968a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S.replace(S.end()-2, S.end(), replace);
2969a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2970a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
297114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (isObjCSelType(PointeeTy)) {
29728baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += ':';
29738baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
2974c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian    }
297585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
297685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
297785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
297885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
2979e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
298085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
298185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
298285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
29836217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
29849533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
29859533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
29869533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
29879533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
29889533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
29899533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
29909533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
29919533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
29929533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
29939533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
29949533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
299585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
299685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
2997a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
2998a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
2999ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
300043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
3001ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3002ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3003ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3004ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
3005ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
3006ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
3007559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
3008559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
3009559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
3010559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
3011559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3012559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3013559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
3014559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
301585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3016559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3017559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
3018559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
3019559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
3020559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3021559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
3022559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
302385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3024559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3025559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3026559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
3027559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
3028ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3029ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3030ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3031ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->getAsFunctionType()) {
3032c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
3033ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3034ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3035ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
30366217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
303782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
3038d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
3039502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
3040502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3041502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
3042502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
3043502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
3044502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
30450d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
30467d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
304717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
304817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   FieldEnd = RDecl->field_end();
304944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
305043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
3051d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
305244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
3053d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
3054d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
3055d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar
3056d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
305743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
305843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
305943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
3060d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
3061a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
3062a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
3063a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getObjCEncodingForTypeImpl(qt, S, false, true,
306443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
3065d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
30667d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
30676de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
3068d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
3069ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3070ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3071ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3072ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
30738b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
30748b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
30758b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
30768b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
3077ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3078ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3079ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3080ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
308121a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
3082ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3083ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3084ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3085ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isObjCInterfaceType()) {
308643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
308743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
308843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
308943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
309043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
309143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
3092f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
309343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
3094f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
309543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
309643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
309743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
309843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
309943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
310043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
310143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
310243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
3103ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
310443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
3105ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3106ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) {
310714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
310814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
310914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3110ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3111ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3112ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCClassType()) {
311314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
311414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3115ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3116ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3117ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
311814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      getObjCEncodingForTypeImpl(getObjCIdType(), S,
311914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
312014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
312114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
312214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
312314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
312414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
312567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
312667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
312714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
312814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
312914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
313014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
313114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
313214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
313314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3134ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3135ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3136ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
3137ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
3138ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
3139ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
3140ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // We encode the underlying type which comes out as
3141ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
3142ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
3143ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      getObjCEncodingForTypeImpl(PointeeTy, S,
3144ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 false, ExpandPointedToStructures,
3145ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
314614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
314714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
3148ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3149ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
3150ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD || EncodingProperty) {
3151ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
315267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      S += OPT->getInterfaceDecl()->getNameAsCString();
315367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
315467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
3155ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
3156ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
3157ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
3158ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      }
3159ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
3160ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3161ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3162ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
3163ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3164ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
316585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
316685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3167a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
3168ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
3169ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
3170ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
3171ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
3172ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
3173ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
3174ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
3175ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
3176ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
3177ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
3178ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
3179ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
3180ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
3181ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
3182ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
3183ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
3184b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
3185b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3186b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
3187b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
3188b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3189ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
3190de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
31917e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
31927e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
3193ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
3194319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCSelType = T;
3195319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
3196319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const TypedefType *TT = T->getAsTypedefType();
3197319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (!TT)
3198319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    return;
3199319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  TypedefDecl *TD = TT->getDecl();
3200b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3201b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  // typedef struct objc_selector *SEL;
32026217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
3203c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!ptr)
3204c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
3205b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
3206c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!rec)
3207c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
3208b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  SelStructType = rec;
3209b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
3210b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3211ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
3212a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
3213390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
3214390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3215ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
3216de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
32178baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
32188baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
3219a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
3220a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCConstantStringType.isNull() &&
32212198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
32222198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
3223a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
32242198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
32252198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
32267532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a qualified
32277532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c std::vector.
32287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
32297532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  bool TemplateKeyword,
32307532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  TemplateDecl *Template) {
32317532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
32327532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
32337532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
32357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName *QTN =
32367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
32377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (!QTN) {
32387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
32397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
32407532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
32417532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32427532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
32437532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
32447532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3245d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
3246d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
3247d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
3248d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                                  bool TemplateKeyword,
3249d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                            OverloadedFunctionDecl *Template) {
3250d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
3251d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3252d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
3253d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
3254d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
3255d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3256d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
3257d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3258d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3259d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
3260d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
3261d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
3262d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
3263d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
32647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
32657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
32667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
32677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
32687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  assert(NNS->isDependent() && "Nested name specifier must be dependent");
32697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
32717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
32727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
32747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
32757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
32767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
32787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
32797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
32817532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
32827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
32837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
32847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
32857532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
32867532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
32877532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32887532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
32897532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
32907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
32917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3292b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
3293d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
3294d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
3295d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas GregorQualType ASTContext::getFromTargetType(unsigned Type) const {
3296b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
3297b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::NoInt: return QualType();
3298b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
3299b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
3300b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
3301b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
3302b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
3303b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
3304b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
3305b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3306b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
3307b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
3308b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
3309b3ac5434ab936f092b8cc48349cb01db3a1e1c76Daniel Dunbar  return QualType();
3310b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
3311b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3312b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3313b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
3314b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3315b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3316fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
3317fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
3318fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
3319f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
3320fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
3321fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
3322fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3323fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
332440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
3325fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
3326fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
3327fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  return false;
3328fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
3329fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
33304fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
33314fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
33324fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
33334fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz JahanianQualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3334b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttrs = QualType::GCNone;
33354fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
33364fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
3337b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
33384fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
3339a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // (or pointers to them) be treated as though they were declared
3340a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
3341a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    if (GCAttrs == QualType::GCNone) {
3342f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff      if (Ty->isObjCObjectPointerType())
3343a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian        GCAttrs = QualType::Strong;
3344a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
33456217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
3346a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
3347c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
3348c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
3349f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
3350c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian      return QualType::GCNone;
33514fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
3352b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
33534fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
33544fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
33556ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
33566ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
33576ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
3358770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
33596ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// areCompatVectorTypes - Return true if the two specified vector types are
33606ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
33616ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
33626ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
33636ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  assert(LHS->isCanonical() && RHS->isCanonical());
33646ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
336561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
33666ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
33676ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
33684084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
33694084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
33704084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
33714084c306635b70f37029dca938444e6013f08684Steve Naroff
33724084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
33734084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
33740fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
33750fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian                                                ObjCProtocolDecl *rProto) {
33764084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
337714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
33784084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
33794084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
33804084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
33814084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
33824084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
33834084c306635b70f37029dca938444e6013f08684Steve Naroff}
338414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
33854084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
33864084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
33874084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
33884084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
33894084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
33904084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
33914084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
33924084c306635b70f37029dca938444e6013f08684Steve Naroff}
33934084c306635b70f37029dca938444e6013f08684Steve Naroff
33944084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
33954084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
33964084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
33974084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
33984084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
33994084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isVoidPointerType() ||
34004084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
34014084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
34024084c306635b70f37029dca938444e6013f08684Steve Naroff  else if (rhs->isVoidPointerType() ||
34034084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
34044084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
34054084c306635b70f37029dca938444e6013f08684Steve Naroff
34064084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
34074084c306635b70f37029dca938444e6013f08684Steve Naroff    const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType();
34084084c306635b70f37029dca938444e6013f08684Steve Naroff
34094084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
34104084c306635b70f37029dca938444e6013f08684Steve Naroff
34114084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
34124084c306635b70f37029dca938444e6013f08684Steve Naroff      // If the RHS is a unqualified interface pointer "NSString*",
34134084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
34144084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
34154084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34164084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
34174084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
34184084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
34194084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
34200fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
34214084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
34224084c306635b70f37029dca938444e6013f08684Steve Naroff        }
34234084c306635b70f37029dca938444e6013f08684Steve Naroff      }
34244084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
34254084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
34264084c306635b70f37029dca938444e6013f08684Steve Naroff    }
3427de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    // Both the right and left sides have qualifiers.
34284084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34294084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
34304084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
34314084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
3432de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
3433de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
3434de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
3435de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
34364084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
34374084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
34384084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
34394084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
34404084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
34414084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
34428f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
34438f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
3444de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
34454084c306635b70f37029dca938444e6013f08684Steve Naroff      // If the RHS is a qualified interface pointer "NSString<P>*",
34464084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
34474084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
34484084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34494084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
34504084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
34514084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
34524084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
34530fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
34544084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
34554084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
34564084c306635b70f37029dca938444e6013f08684Steve Naroff          }
34574084c306635b70f37029dca938444e6013f08684Steve Naroff        }
34584084c306635b70f37029dca938444e6013f08684Steve Naroff      }
34594084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
3460de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
3461de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
34624084c306635b70f37029dca938444e6013f08684Steve Naroff
3463de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
3464de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
34654084c306635b70f37029dca938444e6013f08684Steve Naroff
34664084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
34674084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
34684084c306635b70f37029dca938444e6013f08684Steve Naroff
34694084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsOPT =
34704084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
34714084c306635b70f37029dca938444e6013f08684Steve Naroff    if (lhsOPT->qual_empty()) {
34724084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
34734084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
34744084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
34754084c306635b70f37029dca938444e6013f08684Steve Naroff             E = rhsQID->qual_end(); I != E; ++I) {
34764084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
34774084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
34784084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
34790fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (lhsID->ClassImplementsProtocol(*I, true)) {
34804084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
34814084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
34824084c306635b70f37029dca938444e6013f08684Steve Naroff          }
34834084c306635b70f37029dca938444e6013f08684Steve Naroff        }
34844084c306635b70f37029dca938444e6013f08684Steve Naroff        if (!match)
34854084c306635b70f37029dca938444e6013f08684Steve Naroff          return false;
34864084c306635b70f37029dca938444e6013f08684Steve Naroff      }
34874084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
34884084c306635b70f37029dca938444e6013f08684Steve Naroff    }
34894084c306635b70f37029dca938444e6013f08684Steve Naroff    // Both the right and left sides have qualifiers.
34904084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
34914084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
34924084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
34934084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
34944084c306635b70f37029dca938444e6013f08684Steve Naroff
34954084c306635b70f37029dca938444e6013f08684Steve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
34964084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
34974084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
34984084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
34994084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
35004084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
35014084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
35024084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
35034084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
35044084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
35054084c306635b70f37029dca938444e6013f08684Steve Naroff        }
35064084c306635b70f37029dca938444e6013f08684Steve Naroff      }
35074084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
35084084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
35094084c306635b70f37029dca938444e6013f08684Steve Naroff    }
35104084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
35114084c306635b70f37029dca938444e6013f08684Steve Naroff  }
35124084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
35134084c306635b70f37029dca938444e6013f08684Steve Naroff}
35144084c306635b70f37029dca938444e6013f08684Steve Naroff
35154084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
35164084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
35174084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
35184084c306635b70f37029dca938444e6013f08684Steve Naroff///
35194084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
35204084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
35214084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
35224084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
35234084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
35244084c306635b70f37029dca938444e6013f08684Steve Naroff
35254084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
35264084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
35274084c306635b70f37029dca938444e6013f08684Steve Naroff                                             QualType(RHSOPT,0),
35284084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
35294084c306635b70f37029dca938444e6013f08684Steve Naroff
35304084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
35314084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
35324084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHS && RHS) // We have 2 user-defined types.
35334084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
35344084c306635b70f37029dca938444e6013f08684Steve Naroff
35354084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
353614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
353714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
35383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
35393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman                                         const ObjCInterfaceType *RHS) {
35406ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
35416ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
35426ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
35436ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
35446ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
35456ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
35466ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
3547c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
35486ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
35496ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
35506ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
35516ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
3552c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (RHS->getNumProtocols() == 0)
35536ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
35546ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
3555c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3556c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                        LHSPE = LHS->qual_end();
355791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
355891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
355991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
356091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
356191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
3562c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
35634084c306635b70f37029dca938444e6013f08684Steve Naroff                                          RHSPE = RHS->qual_end();
35648f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
35658f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
356691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
35678f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
35688f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
356991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
357091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
357191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
357291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
357391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
357491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
357591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
35766ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
35776ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
3578389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3579389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
358014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType();
358114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType();
3582389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
358314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
3584389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
358514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
358614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
358714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
3588389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
3589389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
3590ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
3591ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
3592ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// C99 6.2.7p1: Two types have compatible types if their types are the
3593ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
35943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
35953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
35963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
35973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
35983d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
35993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *lbase = lhs->getAsFunctionType();
36003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *rbase = rhs->getAsFunctionType();
360172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
360272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
36033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
36043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
36053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
36073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
36083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
360961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
361061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
361161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
361261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
36136dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  // FIXME: double check this
36142455636163fdd18581d7fdae816433f886d88213Mike Stump  bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
36152455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != lbase->getNoReturnAttr())
36162455636163fdd18581d7fdae816433f886d88213Mike Stump    allLTypes = false;
36172455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != rbase->getNoReturnAttr())
36182455636163fdd18581d7fdae816433f886d88213Mike Stump    allRTypes = false;
36192455636163fdd18581d7fdae816433f886d88213Mike Stump
36203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
3621465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3622465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
36233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
36243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
36253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
36273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
36283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
36293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
36313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
36323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
36333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36347fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
36357fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
36367fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
36373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
36383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
36393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
36403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
36413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
36423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argtype = mergeTypes(largtype, rargtype);
36433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
36443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
364561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
364661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
364761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
364861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
36493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
36503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
36513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
36523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
36532455636163fdd18581d7fdae816433f886d88213Mike Stump                           lproto->isVariadic(), lproto->getTypeQuals(),
36542455636163fdd18581d7fdae816433f886d88213Mike Stump                           NoReturn);
36553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
36563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
36583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
36593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
366072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
36613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
3662465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
36633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
36643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
36653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
36663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
36673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
36683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
36693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
36703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
36713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
36723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
36733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
36743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
36753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
36763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
36783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
36793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
36802d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getNumArgs(), proto->isVariadic(),
36812d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getTypeQuals(), NoReturn);
36823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
36833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
36853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
36862455636163fdd18581d7fdae816433f886d88213Mike Stump  return getFunctionNoProtoType(retType, NoReturn);
36873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
36883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36893d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
369043d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
369143d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
369243d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
36937c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
36947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
36953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // FIXME: C++ shouldn't be going through here!  The rules are different
36963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // enough that they should be handled separately.
36977c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
36987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // shouldn't be going through here!
36996217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
3700c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    LHS = RT->getPointeeType();
37016217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
3702c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    RHS = RT->getPointeeType();
37033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
37053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
37063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3707f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
37083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
37093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
37103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // If the qualifiers are different, the types aren't compatible
37125a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // Note that we handle extended qualifiers later, in the
37135a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // case for ExtQualType.
37145a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
37153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
37163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3717852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
3718852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
3719f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
37201adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
37211adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
37221adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
37231adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
37244c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
372507d258756dc856c6987c394a0972884e6ed46765Eli Friedman  // Strip off objc_gc attributes off the top level so they can be merged.
372607d258756dc856c6987c394a0972884e6ed46765Eli Friedman  // This is a complete mess, but the attribute itself doesn't make much sense.
3727585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  if (RHSClass == Type::ExtQual) {
372807d258756dc856c6987c394a0972884e6ed46765Eli Friedman    QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
372907d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (GCAttr != QualType::GCNone) {
373086f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
37318df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      // __weak attribute must appear on both declarations.
373286f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // __strong attribue is redundant if other decl is an objective-c
373386f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // object pointer (or decorated with __strong attribute); otherwise
373486f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // issue error.
373586f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
373686f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian          (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
373714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff           !LHSCan->isObjCObjectPointerType()))
37388df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        return QualType();
37398df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian
374007d258756dc856c6987c394a0972884e6ed46765Eli Friedman      RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
374107d258756dc856c6987c394a0972884e6ed46765Eli Friedman                     RHS.getCVRQualifiers());
374207d258756dc856c6987c394a0972884e6ed46765Eli Friedman      QualType Result = mergeTypes(LHS, RHS);
37438df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      if (!Result.isNull()) {
37448df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        if (Result.getObjCGCAttr() == QualType::GCNone)
37458df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = getObjCGCQualType(Result, GCAttr);
37468df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        else if (Result.getObjCGCAttr() != GCAttr)
37478df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = QualType();
37488df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      }
374907d258756dc856c6987c394a0972884e6ed46765Eli Friedman      return Result;
375007d258756dc856c6987c394a0972884e6ed46765Eli Friedman    }
3751585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  }
3752585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  if (LHSClass == Type::ExtQual) {
375307d258756dc856c6987c394a0972884e6ed46765Eli Friedman    QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
375407d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (GCAttr != QualType::GCNone) {
37558df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
37568df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      // __weak attribute must appear on both declarations. __strong
375786f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // __strong attribue is redundant if other decl is an objective-c
375886f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // object pointer (or decorated with __strong attribute); otherwise
375986f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // issue error.
376086f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
376186f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian          (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
376214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff           !RHSCan->isObjCObjectPointerType()))
37638df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        return QualType();
376486f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian
376507d258756dc856c6987c394a0972884e6ed46765Eli Friedman      LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
376607d258756dc856c6987c394a0972884e6ed46765Eli Friedman                     LHS.getCVRQualifiers());
376707d258756dc856c6987c394a0972884e6ed46765Eli Friedman      QualType Result = mergeTypes(LHS, RHS);
37688df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      if (!Result.isNull()) {
37698df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        if (Result.getObjCGCAttr() == QualType::GCNone)
37708df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = getObjCGCQualType(Result, GCAttr);
37718df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        else if (Result.getObjCGCAttr() != GCAttr)
37728df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = QualType();
37738df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      }
3774354e53da656237e25518b3fafa3bf84de6e6f57eEli Friedman      return Result;
377507d258756dc856c6987c394a0972884e6ed46765Eli Friedman    }
3776585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  }
3777585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian
37784c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
3779a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3780a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
3781a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3782a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
3783a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner
3784213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
3785213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3786213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
3787ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
37884e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
37894e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
37901adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
37911adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // a signed integer type, or an unsigned integer type.
37923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = LHS->getAsEnumType()) {
37933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
37943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
3795bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
37963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = RHS->getAsEnumType()) {
37973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
37983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
3799bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
38001adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
38013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
3802ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
38033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
38044a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
38051adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
380672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
380772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
380872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
380972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
381072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
381172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
381272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
381372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
38147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
38157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
381672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
381772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
381872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
381972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
382072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
382172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
382272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
382372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
382472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
382572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
382672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
38271adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
38283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
38293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
38306217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
38316217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
38323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
38333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
383407d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
383561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
383607d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
383761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
38383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
38393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
3840c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
3841c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
3842c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
38436217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
38446217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
3845c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3846c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
3847c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3848c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
3849c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3850c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
3851c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
3852c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
38531adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
38543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
38553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
38563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
38573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
38583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
38593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
38603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
38613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
38623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
38633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
386461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
386561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
386661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
386761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
38683bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
38693bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
38703bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
38713bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
38723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
38733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
387461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
387561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
387661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
387761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
38783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
38793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
38803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
38813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
38823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
38833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
38843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
38853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
38863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
38873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
38883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
38893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
38903bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
38913bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
38927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
38937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
38943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
38951adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
38963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return mergeFunctionTypes(LHS, RHS);
389772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
389872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
38993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
39001adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
39013cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
39023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
390364cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
390464cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
390564cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
39063cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
39075a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
39083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
39093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
391061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
391161490e9a965cfee8a78c12c6802138844f04250dCedric Venet  case Type::ObjCInterface: {
39125fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    // Check if the interfaces are assignment compatible.
39135a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
39145a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
39155fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
39165fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
39175fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    if (LHSIface && RHSIface &&
39185fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff        canAssignObjCInterfaces(LHSIface, RHSIface))
39195fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
39205fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
39213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
392261490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
392314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
392414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(),
392514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                RHS->getAsObjCObjectPointerType()))
392614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
392714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
3928bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
392914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
39305a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::FixedWidthInt:
39315a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // Distinct fixed-width integers are not compatible.
39325a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
39335a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::ExtQual:
39345a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: ExtQual types can be compatible even if they're not
39355a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // identical!
39365a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
39375a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // First attempt at an implementation, but I'm not really sure it's
39385a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // right...
39395a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#if 0
39405a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* LQual = cast<ExtQualType>(LHSCan);
39415a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* RQual = cast<ExtQualType>(RHSCan);
39425a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
39435a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman        LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
39445a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return QualType();
39455a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
39465a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    LHSBase = QualType(LQual->getBaseType(), 0);
39475a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    RHSBase = QualType(RQual->getBaseType(), 0);
39485a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = mergeTypes(LHSBase, RHSBase);
39495a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (ResultType.isNull()) return QualType();
39505a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
39515a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LHSCan.getUnqualifiedType() == ResCanUnqual)
39525a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return LHS;
39535a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (RHSCan.getUnqualifiedType() == ResCanUnqual)
39545a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return RHS;
39555a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
39565a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
39575a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
39585a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return ResultType;
39595a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#endif
39607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
39617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  case Type::TemplateSpecialization:
39627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    assert(false && "Dependent types have no size");
39637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
3964ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
396572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
396672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
3967ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
39687192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
39695426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
3970ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
3971ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
397288054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
3973ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
3974ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (T == BoolTy)
3975ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
3976f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3977f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth();
3978f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  }
3979f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
3980ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
3981ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
3982ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
3983ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3984ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
3985ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (const EnumType* ETy = T->getAsEnumType())
3986ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
3987ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  const BuiltinType* BTy = T->getAsBuiltinType();
3988ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert (BTy && "Unexpected signed integer type");
3989ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
3990ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
3991ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
3992ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
3993ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
3994ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
3995ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
3996ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
3997ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
3998ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
3999ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
4000ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
40012df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
40022df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
4003ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
4004ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
4005ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
4006ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
4007ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4008ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
40092cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
40102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
40112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
401286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
401386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
401486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
401586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
401686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
401786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
401886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
401986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters.  This returns the resultant type.
402086df27bbdbb98c39ec2184695c0561209f91beddChris Lattnerstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
402186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
402286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  bool AllowTypeModifiers = true) {
402386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
402486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
402586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
402686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
402786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the modifiers first.
402886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
402986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
403086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
403186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    default: Done = true; --Str; break;
403286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
403386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
403486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
403586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
403686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
403786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
403886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
403986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
404086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
404186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
404286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
404386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
404486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
404586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
404686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
404786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
404886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
404986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
405086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
405186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
405286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
405386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
405486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
405586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
405686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
405786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
405886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
405986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
406086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
406186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
406286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
406386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
406486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
406586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
406686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
406786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
406886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
406986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
407086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
407186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
407286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
407386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
407486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
407586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
407686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
407786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
407886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
407986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
408086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
408186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
408286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
408386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
408486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
408586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
408686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
408786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
408886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
408986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
409086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
409186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
409286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
409386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
409486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
409586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
409686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
409786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
409886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
409986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
410086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
410186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
410286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
410386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
410486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
410586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
410686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
410786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
410886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
410986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
411086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
411186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
411286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
411386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
411486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
411586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
411686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
411786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
411886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
411986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
412086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
412186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
412286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
412386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
412486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Type->isArrayType()) {
412586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
412686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    } else {
412786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
412886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
412986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
413086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
413186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
413286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
413386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
413486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
413586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
413686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
413786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
413886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getVectorType(ElementType, NumElements);
413986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
414086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
41419a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
4142c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
4143c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
4144f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
414586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
414686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
4147fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
41489a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
4149f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
4150782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
4151f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
4152f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
4153f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
4154fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
4155f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
4156fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
4157fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
4158fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
4159782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
416086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
416186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (!AllowTypeModifiers)
416286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return Type;
416386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
416486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Done = false;
416586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
416686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
416786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      default: Done = true; --Str; break;
416886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '*':
416986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getPointerType(Type);
417086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
417186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '&':
417286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getLValueReferenceType(Type);
417386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
417486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      // FIXME: There's no way to have a built-in with an rvalue ref arg.
417586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case 'C':
417686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Type.getQualifiedType(QualType::Const);
417786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
417886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
417986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
418086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
418186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
418286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
418386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
418486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
418586df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id,
418686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                    GetBuiltinTypeError &Error) {
418786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(id);
418886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
418986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
419086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
419186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
419286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
419386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
419486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
419586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
419686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
419786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
419886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
419986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
420086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
420186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
420286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
420386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
420486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
420586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
420686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
420786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
420886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
420986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
421086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
421186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (ArgTypes.size() == 0 && TypeStr[0] == '.')
421286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return getFunctionNoProtoType(ResType);
421386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
421486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                         TypeStr[0] == '.', 0);
421586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
4216a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4217a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType
4218a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4219a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Perform the usual unary conversions. We do this early so that
4220a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // integral promotions to "int" can allow us to exit early, in the
4221a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // lhs == rhs check. Also, for conversion purposes, we ignore any
4222a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // qualifiers.  For example, "const float" and "float" are
4223a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // equivalent.
4224a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isPromotableIntegerType())
4225a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = getPromotedIntegerType(lhs);
4226a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4227a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = lhs.getUnqualifiedType();
4228a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (rhs->isPromotableIntegerType())
4229a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = getPromotedIntegerType(rhs);
4230a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4231a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = rhs.getUnqualifiedType();
4232a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4233a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If both types are identical, no conversion is needed.
4234a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs == rhs)
4235a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
4236a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4237a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4238a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The caller can deal with this (e.g. pointer + int).
4239a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4240a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
4241a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4242a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // At this point, we have two different arithmetic types.
4243a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4244a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Handle complex types first (C99 6.3.1.8p1).
4245a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexType() || rhs->isComplexType()) {
4246a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the complex type.
4247a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
4248a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
4249a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4250a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4251a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
4252a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
4253a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4254a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4255a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // This handles complex/complex, complex/float, or float/complex.
4256a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // When both operands are complex, the shorter operand is converted to the
4257a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // type of the longer, and that is the type of the result. This corresponds
4258a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // to what is done when combining two real floating-point operands.
4259a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The fun begins when size promotion occur across type domains.
4260a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // From H&S 6.3.4: When one operand is complex and the other is a real
4261a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // floating-point type, the less precise type is converted, within it's
4262a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // real or complex domain, to the precision of the other type. For example,
4263a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // when combining a "long double" with a "double _Complex", the
4264a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // "double _Complex" is promoted to "long double _Complex".
4265a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
4266a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4267a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (result > 0) { // The left side is bigger, convert rhs.
4268a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
4269a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (result < 0) { // The right side is bigger, convert lhs.
4270a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
4271a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4272a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // At this point, lhs and rhs have the same rank/size. Now, make sure the
4273a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // domains match. This is a requirement for our implementation, C99
4274a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // does not require this promotion.
4275a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4276a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4277a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return rhs;
4278a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      } else { // handle "_Complex double, double".
4279a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs;
4280a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      }
4281a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4282a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs; // The domain/size match exactly.
4283a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4284a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Now handle "real" floating types (i.e. float, double, long double).
4285a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4286a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the real floating type.
4287a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isIntegerType()) {
4288a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the lhs floating point type.
4289a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4290a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4291a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isComplexIntegerType()) {
4292a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the complex floating point type.
4293a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(lhs);
4294a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4295a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isIntegerType()) {
4296a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the rhs floating point type.
4297a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4298a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4299a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isComplexIntegerType()) {
4300a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the complex floating point type.
4301a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(rhs);
4302a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4303a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // We have two real floating types, float/complex combos were handled above.
4304a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Convert the smaller operand to the bigger result.
4305a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
4306a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (result > 0) // convert the rhs
4307a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4308a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    assert(result < 0 && "illegal float comparison");
4309a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return rhs;   // convert the lhs
4310a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4311a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4312a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Handle GCC complex int extension.
4313a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4314a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4315a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4316a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhsComplexInt && rhsComplexInt) {
4317a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
4318a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman                              rhsComplexInt->getElementType()) >= 0)
4319a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs; // convert the rhs
4320a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4321a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (lhsComplexInt && rhs->isIntegerType()) {
4322a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
4323a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4324a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (rhsComplexInt && lhs->isIntegerType()) {
4325a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
4326a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4327a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4328a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4329a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Finally, we have two differing integer types.
4330a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The rules for this case are in C99 6.3.1.8
4331a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  int compare = getIntegerTypeOrder(lhs, rhs);
4332a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  bool lhsSigned = lhs->isSignedIntegerType(),
4333a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman       rhsSigned = rhs->isSignedIntegerType();
4334a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  QualType destType;
4335a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhsSigned == rhsSigned) {
4336a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Same signedness; use the higher-ranked type
4337a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = compare >= 0 ? lhs : rhs;
4338a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (compare != (lhsSigned ? 1 : -1)) {
4339a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The unsigned type has greater than or equal rank to the
4340a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // signed type, so use the unsigned type
4341a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? rhs : lhs;
4342a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4343a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The two types are different widths; if we are here, that
4344a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // means the signed type is larger than the unsigned type, so
4345a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // use the signed type.
4346a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? lhs : rhs;
4347a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else {
4348a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The signed type is higher-ranked than the unsigned type,
4349a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // but isn't actually any bigger (like unsigned int and long
4350a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // on most 32-bit systems).  Use the unsigned type corresponding
4351a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // to the signed type.
4352a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4353a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4354a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return destType;
4355a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
4356