ASTContext.cpp revision 2d3c191e1d5545e1724ee6e0550c70eef54beff2
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"
18e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2019cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
211b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
22a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
2485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
256fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
26557c5b1717bc8919b1b40cf2064b51491ec53a44Chris Lattner#include "llvm/Support/MemoryBuffer.h"
2729445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson#include "RecordLayoutBuilder.h"
2829445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3561710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
3661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner                       TargetInfo &t,
37e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
381b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
391b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       bool FreeMem, unsigned size_reserve) :
40ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
41c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0),
42c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor  SourceMgr(SM), LangOpts(LOpts),
432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
45e4f2142d00fa5fdb580c4e2413da91882d955381Chris Lattner  BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
46e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  if (size_reserve > 0) Types.reserve(size_reserve);
47e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
4814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
49e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
50e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Deallocate all the types.
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!Types.empty()) {
544b05b1dee6cc65ae61d93dab7edff72710f24589Ted Kremenek    Types.back()->Destroy(*this);
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Types.pop_back();
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
57b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
58b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
59b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
60b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
61b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
62b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
63b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
64b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
65b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
66b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
67b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
68b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar    llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
69b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar      I = ObjCLayouts.begin(), E = ObjCLayouts.end();
70b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
71b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
72b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
73b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
74b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
75b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
76ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
771ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
781ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
791ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNSEnd = NestedNameSpecifiers.end();
80e7dcd782df0e1cc3a63e429a866098e69db39153Douglas Gregor       NNS != NNSEnd;
811ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor       /* Increment in loop */)
821ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
83ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
84ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
85ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
86ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
87b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid
912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
99dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
100dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent) 0,
101dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
102dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
103dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
104dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
105c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
108dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
111dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
112dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
113dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
114dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
115dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
116dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
117dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
118dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
119dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
120dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor
121dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
131f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
14315b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
16564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
1662df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
1672df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
1682df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
1692df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
1703a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
1713a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
1723a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
1733a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
17464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
175f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
176f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
177f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
178f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
179f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
180f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
181f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
182f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
183f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
184f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
1858e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
186898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
187898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
188898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
189898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
190898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
191898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
192898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
193898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
1948e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
195e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // Placeholder type for C++0x auto declarations whose real type has
196e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // not yet been deduced.
197e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
198e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
2038e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2047e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
20514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
206de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
207de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
208de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
209de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
210de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Builtin types for 'id' and 'Class'.
211de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
212de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
21314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
214a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
21533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
21633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
21733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2186e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2196e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2206e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2237caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorVarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) {
2247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
2257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos
2267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
2277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
2287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
2297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
2317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregorvoid
2347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) {
2357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
2367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
2377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
2387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
2397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatedFromStaticDataMember[Inst] = Tmpl;
2407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace {
2432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  class BeforeInTranslationUnit
2442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    : std::binary_function<SourceRange, SourceRange, bool> {
2452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    SourceManager *SourceMgr;
2462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  public:
2482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
2492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    bool operator()(SourceRange X, SourceRange Y) {
2512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
2522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
2532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  };
2542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
2552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Determine whether the given comment is a Doxygen-style comment.
2572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
2582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Start the start of the comment text.
2592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
2602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param End the end of the comment text.
2612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
2622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Member whether we want to check whether this is a member comment
2632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
2642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// we only return true when we find a non-member comment.
2652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorstatic bool
2662e22253e03e175144aeb9d13350a12fd83f858beDouglas GregorisDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
2672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                 bool Member = false) {
2682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *BufferStart
2692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
2702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
2712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
2722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (End - Start < 4)
2742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
2752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  assert(Start[0] == '/' && "Not a comment?");
2772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
2782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
2792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
2802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
2812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return (Start[3] == '<') == Member;
2832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
2842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Retrieve the comment associated with the given declaration, if
2862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// it has one.
2872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorconst char *ASTContext::getCommentForDecl(const Decl *D) {
2882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!D)
2892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
2902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check whether we have cached a comment string for this declaration
2922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // already.
2932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  llvm::DenseMap<const Decl *, std::string>::iterator Pos
2942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = DeclComments.find(D);
2952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Pos != DeclComments.end())
2962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return Pos->second.c_str();
2972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If we have an external AST source and have not yet loaded comments from
2992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // that source, do so now.
3002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (ExternalSource && !LoadedExternalComments) {
3012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::vector<SourceRange> LoadedComments;
3022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ExternalSource->ReadComments(LoadedComments);
3032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!LoadedComments.empty())
3052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      Comments.insert(Comments.begin(), LoadedComments.begin(),
3062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                      LoadedComments.end());
3072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    LoadedExternalComments = true;
3092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
3102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If there are no comments anywhere, we won't find anything.
3122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Comments.empty())
3132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the declaration doesn't map directly to a location in a file, we
3162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // can't find the comment.
3172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  SourceLocation DeclStartLoc = D->getLocStart();
3182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
3192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Find the comment that occurs just before this declaration.
3222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator LastComment
3232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = std::lower_bound(Comments.begin(), Comments.end(),
3242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       SourceRange(DeclStartLoc),
3252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       BeforeInTranslationUnit(&SourceMgr));
3262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the location for the start of the declaration and find the
3282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // beginning of the file buffer.
3292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::pair<FileID, unsigned> DeclStartDecomp
3302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(DeclStartLoc);
3312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *FileBufferStart
3322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(DeclStartDecomp.first).first;
3332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // First check whether we have a comment for a member.
3352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment != Comments.end() &&
3362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
3372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      isDoxygenComment(SourceMgr, *LastComment, true)) {
3382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> LastCommentEndDecomp
3392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(LastComment->getEnd());
3402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
3412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor        SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
3422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor          == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
3432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                                     LastCommentEndDecomp.second)) {
3442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // The Doxygen member comment comes after the declaration starts and
3452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is on the same line and in the same file as the declaration. This
3462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is the comment we want.
3472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      std::string &Result = DeclComments[D];
3482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      Result.append(FileBufferStart +
3492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                      SourceMgr.getFileOffset(LastComment->getBegin()),
3502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                    FileBufferStart + LastCommentEndDecomp.second + 1);
3512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return Result.c_str();
3522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
3532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
3542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment == Comments.begin())
3562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  --LastComment;
3582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the end of the comment.
3602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::pair<FileID, unsigned> LastCommentEndDecomp
3612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(LastComment->getEnd());
3622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment and the declaration aren't in the same file, then they
3642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // aren't related.
3652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartDecomp.first != LastCommentEndDecomp.first)
3662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check that we actually have a Doxygen comment.
3692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!isDoxygenComment(SourceMgr, *LastComment))
3702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Compute the starting line for the declaration and for the end of the
3732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // comment (this is expensive).
3742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned DeclStartLine
3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
3762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned CommentEndLine
3772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
3782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                              LastCommentEndDecomp.second);
3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment does not end on the line prior to the declaration, then
3812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // the comment is not associated with the declaration at all.
3822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (CommentEndLine + 1 != DeclStartLine)
3832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // We have a comment, but there may be more comments on the previous lines.
3862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Keep looking so long as the comments are still Doxygen comments and are
3872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // still adjacent.
3882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned ExpectedLine
3892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
3902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator FirstComment = LastComment;
3912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != Comments.begin()) {
3922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Look at the previous comment
3932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    --FirstComment;
3942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> Decomp
3952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
3962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this previous comment is in a different file, we're done.
3982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Decomp.first != DeclStartDecomp.first) {
3992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this comment is not a Doxygen comment, we're done.
4042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!isDoxygenComment(SourceMgr, *FirstComment)) {
4052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If the line number is not what we expected, we're done.
4102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
4112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Line != ExpectedLine) {
4122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Set the next expected line number.
4172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ExpectedLine
4182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
4192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // The iterator range [FirstComment, LastComment] contains all of the
4222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // BCPL comments that, together, are associated with this declaration.
4232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Form a single comment block string for this declaration that concatenates
4242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // all of these comments.
4252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::string &Result = DeclComments[D];
4262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != LastComment) {
4272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompStart
4282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
4292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompEnd
4302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
4312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    Result.append(FileBufferStart + DecompStart.second,
4322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                  FileBufferStart + DecompEnd.second + 1);
4332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ++FirstComment;
4342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Append the last comment line.
4372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Result.append(FileBufferStart +
4382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                  SourceMgr.getFileOffset(LastComment->getBegin()),
4392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                FileBufferStart + LastCommentEndDecomp.second + 1);
4402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return Result.c_str();
4412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
4422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
443464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
444464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
445464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
446a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
447b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
448b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
449b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
450b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  const BuiltinType *BT = T->getAsBuiltinType();
451b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
452b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
453b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
454b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
455b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
456b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
457b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
458b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
459b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
460af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// getDeclAlign - Return a conservative estimate of the alignment of the
461af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
462af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
463b7d0844c887a72064b624dc6df12cbe1441f69d0Daniel Dunbarunsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
464dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
465dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
46640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
467dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    Align = std::max(Align, AA->getAlignment());
468dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
469af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
470af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
47135366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    if (const ReferenceType* RT = T->getAsReferenceType()) {
4724cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      unsigned AS = RT->getPointeeType().getAddressSpace();
473f0930235ce58a91aa3b840bece9052f44d630536Anders Carlsson      Align = Target.getPointerAlign(AS);
4744cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson    } else if (!T->isIncompleteType() && !T->isFunctionType()) {
4754cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
476dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
477dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
478dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
479dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
480dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
481af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
482dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
483dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  return Align / Target.getCharWidth();
484af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
485b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
486a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
487a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
488d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
4891d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
4905e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
4915e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
492a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
49372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
49472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
49518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
49672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
49772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
49818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
49972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
50072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5015d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
5025d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
50318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
50418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
50518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
50618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
50718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
50872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
509fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
51018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
51118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
51218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
51318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithExpr:
5157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithoutExpr:
516fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
5171d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
518030d8846c7e520330007087e949f621989876e3aChris Lattner
51998be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
5209e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
521030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
522030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
5235c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
524213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
525030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
526030d8846c7e520330007087e949f621989876e3aChris Lattner    std::pair<uint64_t, unsigned> EltInfo =
52798be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<VectorType>(T)->getElementType());
5289e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
5294bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
5306fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
5316fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
5326fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // FIXME: this should probably be a target property.
5336fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    Align = 1 << llvm::Log2_32_Ceil(Align);
534030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
535030d8846c7e520330007087e949f621989876e3aChris Lattner  }
5365d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
5379e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
538a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
539692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
540d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
54118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
54218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
54318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
54418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
54518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5466f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
5479e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
5489e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
5496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
550692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
551692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
552692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
5536f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
5549e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
5559e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
5566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
55764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
55864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
55964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
56064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
561f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
562f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
563f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
564f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
565f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
566f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
567f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
568f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
569692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
5706f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
5719e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
5729e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
5736f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
574692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
5756f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
5769e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
5779e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
5786f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
579692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
5806f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
5819e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
5829e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
5836f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
584692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
5856f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
5869e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
5879e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
5886f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
589ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
590ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
591ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
592ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
593ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
5946f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
5959e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
5969e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
5976f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
5986f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
5995426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
6005426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
6016f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6026f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
6039e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
6049e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
6056f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6066e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
6076e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
6086e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
6091590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
610a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
611bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
612f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  case Type::FixedWidthInt:
613f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // FIXME: This isn't precisely correct; the width/alignment should depend
614f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // on the available types for the target
615f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Width = cast<FixedWidthIntType>(T)->getWidth();
616736166b38235cf6d0ffb67638960d95fb2afcbd6Chris Lattner    Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
617f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Align = Width;
618f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    break;
619f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  case Type::ExtQual:
62098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // FIXME: Pointers into different addr spaces could have different sizes and
62198be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // alignment requirements: getPointerInfo should take an AddrSpace.
622f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
623d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
6245426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
625f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
6266f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
627485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
628485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
629485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
630485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
631485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
632485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
633f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
634f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
6355426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
636f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
637f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
638f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
6397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
6407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
6417ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner    // "When applied to a reference or a reference type, the result is the size
6425d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // of the referenced type." C++98 5.3.3p2: expr.sizeof.
6436f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // FIXME: This is wrong for struct layout: a reference in a struct has
6446f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // pointer size.
645bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
646f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
6471cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
6481cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
6491cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // If we ever want to support other ABIs this needs to be abstracted.
6501cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson
651f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
6521cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    std::pair<uint64_t, unsigned> PtrDiffInfo =
6531cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
6541cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
655f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
656f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
6571cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
6581cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
659f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
6605d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
6615d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
6625d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
6635d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    std::pair<uint64_t, unsigned> EltInfo =
66498be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
6659e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
6665d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
6675d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
6685d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
66944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
6701d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
67144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
67244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
67344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
67444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
67544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
67672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
67772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
6781d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
6791d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
6801d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
6818389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
6828389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
6838389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
6848389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
6858389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner
6861d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
6877176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
6887176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
6891d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
6907176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
6917176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
6927176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
693dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
694a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
6957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
69618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
69718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
69840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
69918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = Aligned->getAlignment();
70018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
70118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
70218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
7037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
7047176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
70518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
70618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
70718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
70818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
70918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
71018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
71118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
71218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
713395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
714395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
715395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
716395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
71718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::QualifiedName:
71818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
71918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
72018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
72118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(getCanonicalType(T) != T &&
72218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
72318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
72418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
72518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
72618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
72718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
728d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
729464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
7309e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
731a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
732a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
73334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
73434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
73534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
73634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
73734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
73834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
7391eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
7401eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
7411eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (const ComplexType* CT = T->getAsComplexType())
7421eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
7431eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
7441eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
7451eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
7461eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
74734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
74834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
74934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
750a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
751a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
752a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
753a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
754a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
755f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
756a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
757a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
758a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
759a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
760a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
761a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
762a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
763a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
764a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
765a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
766a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
767a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
7688e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
7698e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
7708e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
7718e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
7728e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
7738e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 bool CollectSynthesized) {
7748e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
7758e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian         E = OI->ivar_end(); I != E; ++I) {
7768e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian     Ivars.push_back(*I);
7778e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
7788e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  if (CollectSynthesized)
7798e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    CollectSynthesizedIvars(OI, Ivars);
7808e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
7818e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
7829820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
7839820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
78417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
78517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
7869820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
7879820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
7889820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
7899820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into nested protocols.
7909820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
7919820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
7929820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(*P, Ivars);
7939820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
7949820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
7959820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// CollectSynthesizedIvars -
7969820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// This routine collect synthesized ivars for the designated class.
7979820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
7989820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
7999820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
80017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
80117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8029820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
8039820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
8049820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8059820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into interface's protocol list for properties declared
8069820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // in the protocol and whose ivars are synthesized.
8079820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8089820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8099820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8109820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(PD, Ivars);
8119820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8129820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
8139820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
8148e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
8158e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
81617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
81717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
8188e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8198e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8208e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8218e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into nested protocols.
8228e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
8238e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
8248e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(*P);
8258e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8268e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8278e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8288e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI)
8298e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian{
8308e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
83117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
83217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8338e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8348e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8358e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8368e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into interface's protocol list for properties declared
8378e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // in the protocol and whose ivars are synthesized.
8388e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8398e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8408e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8418e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(PD);
8428e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8438e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8448e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8458e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8468a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
8478a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
8488a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8498a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8508a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8518a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
8528a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8538a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
8558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
8568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
8608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
8638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
8648a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
8658a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
8668a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
8678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
8688a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8698a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
8708a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
8718a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
8728a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
8738a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
8748a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8758a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
876b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the
877b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface.
878b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar///
879b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's
880b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars.
88144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout &
882b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
883b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar                          const ObjCImplementationDecl *Impl) {
884532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar  assert(!D->isForwardDecl() && "Invalid interface decl!");
885532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar
88644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Look up this layout, if already laid out, return what we have.
887d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  ObjCContainerDecl *Key =
888d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
889d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
890d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    return *Entry;
89144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
892453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // Add in synthesized ivar count if laying out an implementation.
893453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
89429445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    unsigned FieldCount = D->ivar_size();
8958e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    unsigned SynthCount = CountSynthesizedIvars(D);
8968e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    FieldCount += SynthCount;
897d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    // If there aren't any sythesized ivars then reuse the interface
898453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entry. Note we can't cache this because we simply free all
899453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entries later; however we shouldn't look up implementations
900453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // frequently.
9018e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if (SynthCount == 0)
902453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar      return getObjCLayout(D, 0);
903453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  }
904453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar
90529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  const ASTRecordLayout *NewEntry =
90629445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
90729445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  ObjCLayouts[Key] = NewEntry;
90829445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
90944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  return *NewEntry;
91044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel}
91144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
912b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
913b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
914b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
915b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
916b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
917b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
918b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
919b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
920b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
921b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
92288a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
923464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
924464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
92598be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
9264b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  D = D->getDefinition(*this);
9274b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  assert(D && "Cannot get layout of forward declarations!");
9284bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
929464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
930ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // Note that we can't save a reference to the entry because this function
931ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // is recursive.
932ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  const ASTRecordLayout *Entry = ASTRecordLayouts[D];
933464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
9344bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
93529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  const ASTRecordLayout *NewEntry =
93629445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D);
937ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  ASTRecordLayouts[D] = NewEntry;
93829445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
9395d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
940464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
941464175bba1318bef7905122e9fda20cff926df78Chris Lattner
942a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
943a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
944a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
945a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
946f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
947f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
948f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
949f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
950b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
951b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
952b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
953b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
954b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttr = QualType::GCNone;
955b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
956b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
957b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
958b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // If this type already has an address space specified, it cannot get
959b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
960b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getAddressSpace() == 0 &&
961b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner           "Type cannot be in multiple addr spaces!");
962b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttr = EQT->getObjCGCAttr();
963b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
964b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
965f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner
966b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // Check if we've already instantiated this type.
967ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  llvm::FoldingSetNodeID ID;
9682455636163fdd18581d7fdae816433f886d88213Mike Stump  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
969ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  void *InsertPos = 0;
970f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
971b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
972b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
973ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // If the base type isn't canonical, this won't be a canonical type either,
974ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // so fill in the canonical type field.
975ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  QualType Canonical;
976b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (!TypeNode->isCanonical()) {
977f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    Canonical = getAddrSpaceQualType(CanT, AddressSpace);
978ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
979b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
980f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
981f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
982ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  }
983b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
984b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
985f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
986ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  Types.push_back(New);
987b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
988ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
989ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
990b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
991b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner                                       QualType::GCAttrTypes GCAttr) {
992d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
993b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
994d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
995d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
9964027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  if (T->isPointerType()) {
99735366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    QualType Pointee = T->getAsPointerType()->getPointeeType();
99858f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
9994027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
10004027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
10014027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
10024027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
1003b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
1004b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
1005b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
1006b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
1007b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned AddressSpace = 0;
1008b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
1009b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
10102455636163fdd18581d7fdae816433f886d88213Mike Stump    // If this type already has an ObjCGC specified, it cannot get
1011b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
1012b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getObjCGCAttr() == QualType::GCNone &&
10132455636163fdd18581d7fdae816433f886d88213Mike Stump           "Type cannot have multiple ObjCGCs!");
1014b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    AddressSpace = EQT->getAddressSpace();
1015b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
1016b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
1017d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1018d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check if we've already instantiated an gc qual'd type of this type.
1019d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  llvm::FoldingSetNodeID ID;
10202455636163fdd18581d7fdae816433f886d88213Mike Stump  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
1021d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  void *InsertPos = 0;
1022d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
1023b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
1024d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1025d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // If the base type isn't canonical, this won't be a canonical type either,
1026d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // so fill in the canonical type field.
10275a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // FIXME: Isn't this also not canonical if the base type is a array
10285a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // or pointer type?  I can't find any documentation for objc_gc, though...
1029d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType Canonical;
1030d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (!T->isCanonical()) {
1031b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    Canonical = getObjCGCQualType(CanT, GCAttr);
1032d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
1033b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
1034d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1035d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1036d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1037b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
1038b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
1039d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
1040d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  Types.push_back(New);
1041b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
1042d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1043a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
10442455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getNoReturnType(QualType T) {
10456dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  QualifierSet qs;
10466dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  qs.strip(T);
10472455636163fdd18581d7fdae816433f886d88213Mike Stump  if (T->isPointerType()) {
10482455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType Pointee = T->getAsPointerType()->getPointeeType();
10492455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType ResultType = getNoReturnType(Pointee);
10506dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getPointerType(ResultType);
10516dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType.setCVRQualifiers(T.getCVRQualifiers());
10526dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    return qs.apply(ResultType, *this);
10532455636163fdd18581d7fdae816433f886d88213Mike Stump  }
10542455636163fdd18581d7fdae816433f886d88213Mike Stump  if (T->isBlockPointerType()) {
10552455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType Pointee = T->getAsBlockPointerType()->getPointeeType();
10562455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType ResultType = getNoReturnType(Pointee);
10576dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getBlockPointerType(ResultType);
10586dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType.setCVRQualifiers(T.getCVRQualifiers());
10596dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    return qs.apply(ResultType, *this);
10602455636163fdd18581d7fdae816433f886d88213Mike Stump  }
10612455636163fdd18581d7fdae816433f886d88213Mike Stump  if (!T->isFunctionType())
10622455636163fdd18581d7fdae816433f886d88213Mike Stump    assert(0 && "can't noreturn qualify non-pointer to function or block type");
10632455636163fdd18581d7fdae816433f886d88213Mike Stump
10642455636163fdd18581d7fdae816433f886d88213Mike Stump  if (const FunctionNoProtoType *F = dyn_cast<FunctionNoProtoType>(T)) {
10652455636163fdd18581d7fdae816433f886d88213Mike Stump    return getFunctionNoProtoType(F->getResultType(), true);
10662455636163fdd18581d7fdae816433f886d88213Mike Stump  }
10672455636163fdd18581d7fdae816433f886d88213Mike Stump  const FunctionProtoType *F = cast<FunctionProtoType>(T);
10682455636163fdd18581d7fdae816433f886d88213Mike Stump  return getFunctionType(F->getResultType(), F->arg_type_begin(),
10692455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
10702455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
10712455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->getNumExceptions(), F->exception_begin(), true);
10722455636163fdd18581d7fdae816433f886d88213Mike Stump}
10732455636163fdd18581d7fdae816433f886d88213Mike Stump
10745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
10855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1090f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
10915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1094f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1096f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ComplexType *New = new (*this,8) ComplexType(T, Canonical);
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
10995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1102f98aba35e6c3da5aae61843fc01334939e4e12ecEli FriedmanQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1103f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1104f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman     SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1105f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  FixedWidthIntType *&Entry = Map[Width];
1106f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (!Entry)
1107f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Entry = new FixedWidthIntType(Width, Signed);
1108f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  return QualType(Entry, 0);
1109f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman}
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1127f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1131f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1133f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  PointerType *New = new (*this,8) PointerType(T, Canonical);
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11395618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// getBlockPointerType - Return the uniqued reference to the type for
11405618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
11415618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
1142296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1143296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
11445618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
11455618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
11465618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
11475618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11485618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
11495618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
11505618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11515618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
11525618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
1153296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // If the block pointee type isn't canonical, this won't be a canonical
11545618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
11555618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
11565618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (!T->isCanonical()) {
11575618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
11585618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11595618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
11605618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
11615618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1162f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11635618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
1164f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
11655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
11665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
11675618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
11685618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
11695618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
11707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
11717c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
11727c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getLValueReferenceType(QualType T) {
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceType::Profile(ID, T);
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
11807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
11827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
11877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getLValueReferenceType(getCanonicalType(T));
11887c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
11897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
11907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
11917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
11927c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11937c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
11947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
11957c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
11967c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
11977c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
11987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
11997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
12007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
12027c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
12037c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
12047c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
12057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
12067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
12077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  ReferenceType::Profile(ID, T);
12087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12097c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
12107c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
12117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
12137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
12157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
12167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
12177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (!T->isCanonical()) {
12187c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getRValueReferenceType(getCanonicalType(T));
12197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
12227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1223f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1232f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1233f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
1234f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian RedlQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1235f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl{
1236f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1237f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1238f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1239f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1240f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1241f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1242f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1243f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1244f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1245f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1246f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1247f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1248f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
1249f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (!T->isCanonical()) {
1250f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1251f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1252f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1253f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1254f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1255f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1256f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
1257f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
1258f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1259f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1260f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1261f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1262f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1263fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// getConstantArrayType - Return the unique reference to the type for an
1264fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
1265fb22d96692c5240fb8d611290dbf7eeed3759c73Steve NaroffQualType ASTContext::getConstantArrayType(QualType EltTy,
126638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1267c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1268c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1269587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman  assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1270587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1271587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
127238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
127338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
127438aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
127538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
127638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner
12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
12780be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
12817192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  if (ConstantArrayType *ATP =
12827192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
12875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!EltTy->isCanonical()) {
1289f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1290c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
12915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12927192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek    ConstantArrayType *NewIP =
12937192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1294f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1297566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  ConstantArrayType *New =
1298f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
12997192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
13005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13047e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithExprType - Return a reference to the type for
13057e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13067e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithExprType(QualType EltTy,
13087e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         const llvm::APInt &ArySizeIn,
13097e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         Expr *ArySizeExpr,
13107e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         ArrayType::ArraySizeModifier ASM,
13117e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         unsigned EltTypeQuals,
13127e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         SourceRange Brackets) {
13137e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13177e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13187e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
13197e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
13207e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
13217e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Since we don't unique expressions, it isn't possible to unique VLA's
13227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // that have an expression provided for their size.
13237e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithExprType *New =
13247e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
13257e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ArySize, ArySizeExpr,
13267e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ASM, EltTypeQuals, Brackets);
13277e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
13287e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
13297e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
13307e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithoutExprType - Return a reference to the type for
13327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13347e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithoutExprType(QualType EltTy,
13357e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            const llvm::APInt &ArySizeIn,
13367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArrayType::ArraySizeModifier ASM,
13377e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            unsigned EltTypeQuals) {
13387e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13397e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13417e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13427e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13437e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
13447e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
13457e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
13467e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithoutExprType *New =
13477e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
13487e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                             ArySize, ASM, EltTypeQuals);
13497e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
13507e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
13517e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
13527e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
1353bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1354bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
13557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
13567e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1357c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
13587e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          unsigned EltTypeQuals,
13597e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          SourceRange Brackets) {
1360c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1361c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1362c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1363566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  VariableArrayType *New =
13647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)VariableArrayType(EltTy, QualType(),
13657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  NumElts, ASM, EltTypeQuals, Brackets);
1366c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1367c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1368c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1369c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1370c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1371c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1372898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1373898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
1374898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// type. FIXME: We will need these to be uniqued, or at least
1375898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// comparable, at some point.
13767e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy,
13777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                Expr *NumElts,
1378898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
13797e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                unsigned EltTypeQuals,
13807e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                SourceRange Brackets) {
1381898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1382898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1383898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1384898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Since we don't unique expressions, it isn't possible to unique
1385898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // dependently-sized array types.
1386898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1387566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  DependentSizedArrayType *New =
13887e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new (*this,8) DependentSizedArrayType(EltTy, QualType(),
13897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          NumElts, ASM, EltTypeQuals,
13907e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Brackets);
1391898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1392898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  DependentSizedArrayTypes.push_back(New);
1393898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1394898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1395898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1396898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1397c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1398c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1399c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1400c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
14010be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1402c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1403c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
1404c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (IncompleteArrayType *ATP =
1405c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1406c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1407c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1408c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1409c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1410c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1411c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1412c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (!EltTy->isCanonical()) {
1413f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
14142bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1415c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1416c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1417c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1418c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1419f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14202bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1421c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
14227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  IncompleteArrayType *New
14237e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    = new (*this,8) IncompleteArrayType(EltTy, Canonical,
14247e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                        ASM, EltTypeQuals);
1425c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1426c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1427c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1428c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1429fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1430fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
143173322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
143273322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
143373322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1436f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
143773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
144173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorType::Profile(ID, vecType, NumElts, Type::Vector);
14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!vecType->isCanonical()) {
1450f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getVectorType(getCanonicalType(vecType), NumElts);
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1454f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1456f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1462213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
146373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1464213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
146573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
146673322924127c873c13101b705dd823f5539ffa5fSteve Naroff
1467f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1468213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
146973322924127c873c13101b705dd823f5539ffa5fSteve Naroff
147073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
147173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
1472213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
147373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
147473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
147573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
147673322924127c873c13101b705dd823f5539ffa5fSteve Naroff
147773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
147873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
147973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
148073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (!vecType->isCanonical()) {
1481213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
148273322924127c873c13101b705dd823f5539ffa5fSteve Naroff
148373322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
148473322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1485f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
148673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
1487f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
148873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
148973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
149073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
149173322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
149273322924127c873c13101b705dd823f5539ffa5fSteve Naroff
14939cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas GregorQualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
14949cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    Expr *SizeExpr,
14959cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    SourceLocation AttrLoc) {
14969cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  DependentSizedExtVectorType *New =
14979cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor      new (*this,8) DependentSizedExtVectorType(vecType, QualType(),
14989cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                SizeExpr, AttrLoc);
14999cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
15009cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  DependentSizedExtVectorTypes.push_back(New);
15019cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
15029cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
15039cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
150572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
15072455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
15112455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
151472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (FunctionNoProtoType *FT =
151572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
15165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
15175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!ResultTy->isCanonical()) {
15202455636163fdd18581d7fdae816433f886d88213Mike Stump    Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
152372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
152472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1525f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15282455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType *New
15292455636163fdd18581d7fdae816433f886d88213Mike Stump    = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn);
15305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
153172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
153761710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1538971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1539465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     unsigned TypeQuals, bool hasExceptionSpec,
1540465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     bool hasAnyExceptionSpec, unsigned NumExs,
15412455636163fdd18581d7fdae816433f886d88213Mike Stump                                     const QualType *ExArray, bool NoReturn) {
15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
154572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1546465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                             TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
15472455636163fdd18581d7fdae816433f886d88213Mike Stump                             NumExs, ExArray, NoReturn);
15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
155072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (FunctionProtoType *FTP =
155172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
15525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1553465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1554465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
15555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCanonical = ResultTy->isCanonical();
1556465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  if (hasExceptionSpec)
1557465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    isCanonical = false;
15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
15595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!ArgArray[i]->isCanonical())
15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
15615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
1563465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
15645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
15655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isCanonical) {
15665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
15675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
15685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
1569f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner      CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
1570465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1571f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1572beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
15732455636163fdd18581d7fdae816433f886d88213Mike Stump                                isVariadic, TypeQuals, NoReturn);
1574465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
15755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
157672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
157772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1578f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1580465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
158172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1582465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
1583465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // end of them.
158472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType *FTP =
1585465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1586465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumArgs*sizeof(QualType) +
1587465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumExs*sizeof(QualType), 8);
158872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1589465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                              TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
15902455636163fdd18581d7fdae816433f886d88213Mike Stump                              ExArray, NumExs, Canonical, NoReturn);
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
159272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15962ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
15972ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
15984b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
15991e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
16002ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
16012ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
16021e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
16032ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1604fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  else if (isa<TemplateTypeParmDecl>(Decl)) {
1605fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    assert(false && "Template type parameter types are always available.");
1606fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
16072ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getObjCInterfaceType(ObjCInterface);
160849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1609c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1610566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1611566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1612f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1613f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) RecordType(Record);
1614566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  }
1615566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1616566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1617566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1618f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1619f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) EnumType(Enum);
16204b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  }
162149aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  else
16222ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    assert(false && "TypeDecl without a type?");
162349aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
16244b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
162549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
16262ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
16272ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
16285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
16305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) {
16315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1633f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
163472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
16355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
16375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1639fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
164076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// parameter or parameter pack with the given depth, index, and (optionally)
164176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
1642fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas GregorQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
164376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
1644fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1645fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
164676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
1647fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
1648fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmType *TypeParm
1649fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1650fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1651fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1652fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
1653fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
165476e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  if (Name) {
165576e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
165676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
165776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                                   Name, Canon);
165876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
165976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
1660fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1661fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1662fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1663fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1664fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1665fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1666fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
166755f6b14230c94272efbbcdd89a92224c8db9f225Douglas GregorQualType
16687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
16697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
16707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
16717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          QualType Canon) {
167240808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor  if (!Canon.isNull())
167340808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    Canon = getCanonicalType(Canon);
1674fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
167555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  llvm::FoldingSetNodeID ID;
16767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
167740808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor
167855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  void *InsertPos = 0;
16797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  TemplateSpecializationType *Spec
16807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
168155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
168255f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  if (Spec)
168355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    return QualType(Spec, 0);
168455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
16857532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
168640808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
168740808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                       8);
16887532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
168955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
16907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
169155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
169255f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  return QualType(Spec, 0);
169355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
169455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
1695e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas GregorQualType
1696ab452ba8323d1985e08bade2bced588cddf2cc28Douglas GregorASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
1697e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor                                 QualType NamedType) {
1698e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1699ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  QualifiedNameType::Profile(ID, NNS, NamedType);
1700e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1701e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
1702e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameType *T
1703e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1704e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1705e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1706e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1707ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  T = new (*this) QualifiedNameType(NNS, NamedType,
1708ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor                                    getCanonicalType(NamedType));
1709e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1710e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameTypes.InsertNode(T, InsertPos);
1711e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1712e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1713e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1714d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1715d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     const IdentifierInfo *Name,
1716d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     QualType Canon) {
1717d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1718d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1719d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1720d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1721d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (CanonNNS != NNS)
1722d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Canon = getTypenameType(CanonNNS, Name);
1723d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1724d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1725d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
1726d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType::Profile(ID, NNS, Name);
1727d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1728d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
1729d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType *T
1730d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1731d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1732d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1733d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1734d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  T = new (*this) TypenameType(NNS, Name, Canon);
1735d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
1736d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
1737d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return QualType(T, 0);
1738d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1739d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
17401734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorQualType
17411734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorASTContext::getTypenameType(NestedNameSpecifier *NNS,
17421734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            const TemplateSpecializationType *TemplateId,
17431734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            QualType Canon) {
17441734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
17451734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
17461734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
17471734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
17481734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
17491734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
17501734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
17511734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        = CanonType->getAsTemplateSpecializationType();
17521734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
17531734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
17541734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Canon = getTypenameType(CanonNNS, CanonTemplateId);
17551734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
17561734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
17571734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
17581734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  llvm::FoldingSetNodeID ID;
17591734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType::Profile(ID, NNS, TemplateId);
17601734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
17611734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  void *InsertPos = 0;
17621734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType *T
17631734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
17641734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (T)
17651734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    return QualType(T, 0);
17661734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
17671734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  T = new (*this) TypenameType(NNS, TemplateId, Canon);
17681734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
17691734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
17701734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  return QualType(T, 0);
17711734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
17721734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
177388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
177488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
177588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
177688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
17772e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
177888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
177988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
178088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
178188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
178288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
178388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
178488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
178588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
178688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
178788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
178888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
178988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
179088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
179188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1792d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1793d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// the given interface decl and the conforming protocol list.
179414108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
1795d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff                                              ObjCProtocolDecl **Protocols,
1796d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff                                              unsigned NumProtocols) {
1797d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // Sort the protocol list alphabetically to canonicalize it.
1798d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (NumProtocols)
1799d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
1800d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1801d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  llvm::FoldingSetNodeID ID;
180214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
1803d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1804d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
1805d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (ObjCObjectPointerType *QT =
1806d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1807d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    return QualType(QT, 0);
1808d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1809d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // No Match;
1810d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerType *QType =
181114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
1812d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1813d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  Types.push_back(QType);
1814d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1815d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  return QualType(QType, 0);
1816d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
181788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1818c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// getObjCInterfaceType - Return the unique reference to the type for the
1819c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// specified ObjC interface decl. The list of protocols is optional.
1820c15cb2af27514ecc879daba9aa01389c5203685dSteve NaroffQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
1821a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                       ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
1822c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (NumProtocols)
1823c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    // Sort the protocol list alphabetically to canonicalize it.
1824c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
182588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
18264b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
1827c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
18284b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
18294b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
1830c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (ObjCInterfaceType *QT =
1831c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff      ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
18324b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
18334b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
18344b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  // No Match;
1835c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType *QType =
1836c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1837c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                    Protocols, NumProtocols);
18384b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  Types.push_back(QType);
1839c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceTypes.InsertNode(QType, InsertPos);
18404b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  return QualType(QType, 0);
18414b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian}
18424b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
184372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
184472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
18459752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
18469752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// DeclRefExpr's. This doesn't effect the type checker, since it operates
18479752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
184872564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
1849dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
1850dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  if (tofExpr->isTypeDependent())
1851dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    toe = new (*this, 8) TypeOfExprType(tofExpr);
1852dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  else {
1853dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
1854dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
1855dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
18569752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
18579752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
1858d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
1859d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
18609752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
18619752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
18629752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
18639752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// an issue. This doesn't effect the type checker, since it operates
18649752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
1865d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
1866f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
1867f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
18689752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
18699752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
1870d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
1871d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
187260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
187360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
187460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
1875a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
1876a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
1877a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson
187860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
187960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
188060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
188160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
188260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
188360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
188460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
188560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
188660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
188760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
188860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
188960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
189060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
189160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
189260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
189360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
189460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
189560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
189660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
189760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
189860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (e->isLvalue(Context) == Expr::LV_Valid)
189960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
190060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
190160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
190260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
190360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
1904395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
1905395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
1906395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
1907395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// an issue. This doesn't effect the type checker, since it operates
1908395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
1909395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) {
1910dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
1911dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  if (e->isTypeDependent()) // FIXME: canonicalize the expression
1912563a03b1338d31c2462def43253a722bc885d384Anders Carlsson    dt = new (*this, 8) DecltypeType(e, DependentTy);
1913dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  else {
1914dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
1915563a03b1338d31c2462def43253a722bc885d384Anders Carlsson    dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
1916dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
1917395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
1918395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
1919395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
1920395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
19215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
19225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
19235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTagDeclType(TagDecl *Decl) {
1924d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
19252ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  return getTypeDeclType(Decl);
19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
19295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// needs to agree with the definition in <stddef.h>.
19315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const {
1932b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
19335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
193564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
193664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
193764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
193864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
193964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
194064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
194164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
194264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
194364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
194464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
194564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
194664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
194764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
194864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
19498b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
19508b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
19518b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
1952b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
19538b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
19548b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
1955e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
1956e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
1957e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
1958e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
195977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
196077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
196177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
196277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
196377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
196477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
196577c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris LattnerQualType ASTContext::getCanonicalType(QualType T) {
196677c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner  QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
1967c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1968c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the result has type qualifiers, make sure to canonicalize them as well.
1969c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1970c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (TypeQuals == 0) return CanType;
1971c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1972c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the type qualifiers are on an array type, get the canonical type of the
1973c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // array with the qualifiers applied to the element type.
1974c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
1975c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
1976c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return CanType.getQualifiedType(TypeQuals);
1977c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1978c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
1979c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
1980c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1981c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
1982c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1983c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1984c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1985c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                CAT->getIndexTypeQualifier());
1986c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1987c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1988c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                  IAT->getIndexTypeQualifier());
1989c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1990898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
19917e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getDependentSizedArrayType(NewEltTy,
19927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                      DSAT->getSizeExpr(),
1993898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                      DSAT->getSizeModifier(),
19947e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                      DSAT->getIndexTypeQualifier(),
19957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                      DSAT->getBracketsRange());
1996898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1997c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
19987e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return getVariableArrayType(NewEltTy,
19997e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              VAT->getSizeExpr(),
2000c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                              VAT->getSizeModifier(),
20017e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              VAT->getIndexTypeQualifier(),
20027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                              VAT->getBracketsRange());
2003c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
2004c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
200525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
200625a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
200725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
200825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
200997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
201025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
201125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
201225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
201325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
201425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
201525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
2016d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
2017d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
2018d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (!NNS)
2019d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
2020d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2021d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
2022d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
2023d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
2024d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this,
2025d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2026d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
2027d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2028d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
2029d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
2030d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
2031d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2032d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2033d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
2034d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
2035d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
2036d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *Prefix = 0;
2037d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2038d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // FIXME: This isn't the right check!
2039d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (T->isDependentType())
2040d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
2041d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2042d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, Prefix,
2043d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2044d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
2045d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2046d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2047d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
2048d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
2049d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
2050d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2051d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2052d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
2053d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
2054d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2055d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2056c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2057c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
2058c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
2059c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (T.getCVRQualifiers() == 0) {
2060c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
2061c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2062c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
2063c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
2064c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2065c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the common negative case fast, ignoring CVR qualifiers.
2066c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
2067c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2068f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Make sure to look through type qualifiers (like ExtQuals) for the negative
2069c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // test.
2070c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!isa<ArrayType>(CType) &&
2071c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      !isa<ArrayType>(CType.getUnqualifiedType()))
2072c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
2073c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2074c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Apply any CVR qualifiers from the array type to the element type.  This
2075c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
2076c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
2077c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2078c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
2079c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
2080c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // we must propagate them down into the elemeng type.
2081c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
2082c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned AddrSpace = 0;
2083c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  Type *Ty = T.getTypePtr();
2084c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2085f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Rip through ExtQualType's and typedefs to get to a concrete type.
2086c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  while (1) {
2087f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
2088f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      AddrSpace = EXTQT->getAddressSpace();
2089f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      Ty = EXTQT->getBaseType();
2090c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    } else {
2091c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      T = Ty->getDesugaredType();
2092c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
2093c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner        break;
2094c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      CVRQuals |= T.getCVRQualifiers();
2095c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      Ty = T.getTypePtr();
2096c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    }
2097c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
2098c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2099c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
2100c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2101c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
2102c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
2103c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2104c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
2105c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
2106c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2107c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
2108c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy = ATy->getElementType();
2109c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (AddrSpace)
2110f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
2111c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
2112c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2113c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2114c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2115c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
2116c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getIndexTypeQualifier()));
2117c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2118c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2119c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
21207e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                  IAT->getIndexTypeQualifier()));
2121898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2122898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (const DependentSizedArrayType *DSAT
2123898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
2124898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
2125898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                     getDependentSizedArrayType(NewEltTy,
2126898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeExpr(),
2127898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
21287e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getIndexTypeQualifier(),
21297e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
2130c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2131c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
21327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
21337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getSizeExpr(),
2134c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
21357e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getIndexTypeQualifier(),
21367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
213777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
213877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
213977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
2140e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
2141e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
2142e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
2143e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
2144e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
2145e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2146e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
2147c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
2148c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
2149c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
2150c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
2151c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2152c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
2153e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2154c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
2155e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2156e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
2157c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
2158e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
2159e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
21605e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) {
21615e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  QualifierSet qualifiers;
21625e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  while (true) {
21635e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    const Type *UT = qualifiers.strip(QT);
21645e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
21655e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor      QT = AT->getElementType();
21666dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    } else {
21675e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor      return qualifiers.apply(QT, *this);
21685e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    }
21695e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  }
21705e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
21715e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
2172d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel DunbarQualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
21736183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  QualType ElemTy = VAT->getElementType();
21746183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
21756183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
21766183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson    return getBaseElementType(VAT);
21776183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
21786183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
21796183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
21806183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
21825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
2183a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
2184ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  if (const ComplexType *CT = T->getAsComplexType())
21855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
2186a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2187d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel Dunbar  assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
2188ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  switch (T->getAsBuiltinType()->getKind()) {
2189a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
21905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
21915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
21925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
21935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2196716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2197716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// point or a complex type (based on typeDomain/typeSize).
2198716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
2199716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
22001361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
22011361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
22021361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
22031361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
22041361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
2205716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
2206f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
2207f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
2208f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
2209f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
2210f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
22111361b11066239ea15764a2a844405352d87296b3Chris Lattner
22121361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
22131361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
22141361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
22151361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
22161361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
22171361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
22185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
22227cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
22237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
22247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
2225a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2226a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
2227a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
2228a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2229a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
2230fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
2231a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
2232fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
2233fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2236f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2237f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
2238f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
2239f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
2240f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  assert(T->isCanonical() && "T should be canonicalized");
2241f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
2242f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    T = ET->getDecl()->getIntegerType().getTypePtr();
2243f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2244a34267595534a72703290153a6f7e3da1adcec59Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::WChar))
2245a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
2246a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
2247f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
2248f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2249f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2250f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
2251f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2252f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2253f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // There are two things which impact the integer rank: the width, and
2254f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // the ordering of builtins.  The builtin ordering is encoded in the
2255f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // bottom three bits; the width is encoded in the bits above that.
22561b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
2257f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth() << 3;
2258f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2259f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
22607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
22617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
2262f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
22637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
22647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
22657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
22667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
2267f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
22687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
22697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
2270f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
22717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
22727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
2273f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
22747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
22757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
2276f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
22777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
22787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
2279f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
22802df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
22812df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
22822df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
2283f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
2284f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
2285f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
22867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getIntegerTypeOrder - Returns the highest ranked integer type:
22877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
22887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
22897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
2290f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
2291f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
22927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
22935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2294f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2295f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
22965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22977cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
22987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
22995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
23017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
23027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
23037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
23047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
23057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
23067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
23077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
23087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
23097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
23107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
23117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
23127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
23137cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // powers of two larger than each other, this is always safe.
23147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
23157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
23167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
23177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
23187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
23197cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
23205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
23227cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
23237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // powers of two larger than each other, this is always safe.
23247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
23255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
232671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
232771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson// getCFConstantStringType - Return the type used for constant CFStrings.
232871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
232971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
23306c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner    CFConstantStringTypeDecl =
233139ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2332df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek                         &Idents.get("NSConstantString"));
2333f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
233471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
233571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
233671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
2337f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2338f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
233971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
2340f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
234171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
2342f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[3] = LongTy;
234371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
234444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
234544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
234644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
234744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
234844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           FieldTypes[i], /*BitWidth=*/0,
23494afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
235017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
235144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
235244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
235344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    CFConstantStringTypeDecl->completeDefinition(*this);
235471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
235571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
235671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
23578467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2358b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2359319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
236035366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  const RecordType *Rec = T->getAsRecordType();
2361319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2362319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2363319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2364319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2365bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders CarlssonQualType ASTContext::getObjCFastEnumerationStateType()
2366bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson{
2367bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
236844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
236944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
237044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                         &Idents.get("__objcFastEnumerationState"));
237144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
2372bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2373bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2374de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
2375bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2376bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2377bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2378bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
2379bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
238044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
238144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      FieldDecl *Field = FieldDecl::Create(*this,
238244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           ObjCFastEnumerationStateTypeDecl,
238344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
238444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           FieldTypes[i], /*BitWidth=*/0,
23854afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
238617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
238744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
2388bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
238944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
2390bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
2391bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2392bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2393bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2394bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2395319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
239635366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  const RecordType *Rec = T->getAsRecordType();
2397319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2398319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2399319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2400319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2401e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
2402e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
24032d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
2404e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
2405bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2406bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
240785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
240885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
240985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
241085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2411a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
241233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
2413a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) {
241498be4943e8dc4f3905629a7102668960873cf863Chris Lattner  uint64_t sz = getTypeSize(type);
241533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
241633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
241733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (sz > 0 && type->isIntegralType())
241898be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = std::max(sz, getTypeSize(IntTy));
241933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
242033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
242198be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = getTypeSize(VoidPtrTy);
242298be4943e8dc4f3905629a7102668960873cf863Chris Lattner  return sz / getTypeSize(CharTy);
242333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
242433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2425a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
242633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
2427c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
2428e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
2429c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2430ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
2431a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
243233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
24330d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
243433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
243533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
243633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
243733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
243898be4943e8dc4f3905629a7102668960873cf863Chris Lattner  int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
243933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
244033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
244133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  int ParmOffset = 2 * PtrSize;
244289951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
244389951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
244489951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
244589951a86b594513c2a013532ed45d197413b1087Chris Lattner    int sz = getObjCEncodingTypeSize(PType);
2446a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
244733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
244833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
244933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(ParmOffset);
245033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
245133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(PtrSize);
245233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
245333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
245433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
245589951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
245689951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
245789951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
24584306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    QualType PType = PVDecl->getOriginalType();
24594306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
2460ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2461ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
2462ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
2463bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
2464ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
2465ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
2466ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
2467ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
246833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
24694306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
24700d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
247133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    S += llvm::utostr(ParmOffset);
2472a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ParmOffset += getObjCEncodingTypeSize(PType);
247333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
247433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
247533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2476c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
247783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
2478c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2479c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
248083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// Property attributes are stored as a comma-delimited C string. The simple
248183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// attributes readonly and bycopy are encoded as single characters. The
248283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// parametrized attributes, getter=name, setter=name, and ivar=name, are
248383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// encoded as single characters, followed by an identifier. Property types
248483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// are also encoded as a parametrized attribute. The characters used to encode
248583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
248683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
248783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
248883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
248983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
249083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
249183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
249283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
249383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
249483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
249583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
249683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
249783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
249883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
249983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
250083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
2501c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2502c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
2503e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
2504c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
2505c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
2506c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
2507c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2508c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
2509c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
2510c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    if (const ObjCCategoryImplDecl *CID =
2511c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
2512c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
251317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
2514653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2515c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2516c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2517c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2518c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2519c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2520c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2521c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2522c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2523c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2524c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
252561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
2526c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
252717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
2528653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2529c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2530c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2531c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2532c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2533c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2534c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2535c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2536c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2537c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2538c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2539c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2540c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2541c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2542c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
2543c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2544c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
2545090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
2546090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
2547153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
2548090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
2549090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
2550c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2551c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
2552c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
2553c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
2554c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
2555c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
2556c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
2557c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Retain: S += ",&"; break;
2558c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2559c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2560c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2561c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
2562c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
2563c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
2564c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
2565c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2566090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2567090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
2568090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian
2569c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2570c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
2571077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
2572c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2573c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2574c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2575c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
2576077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
2577c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2578c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2579c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
2580c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2581c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
258239f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
2583c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2584c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2585c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
2586c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
2587c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2588a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
2589a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// Another legacy compatibility encoding: 32-bit longs are encoded as
2590c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian/// 'l' or 'L' , but not always.  For typedefs, we need to use
2591a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2592a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
2593a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
25948e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
2595a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
2596c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
2597c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2598a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
2599c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      else
2600c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
2601c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2602a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
2603a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2604a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
2605a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
2606a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
26077d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
2608153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
260982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
261082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
261182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
261282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
26135b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian  getObjCEncodingForTypeImpl(T, S, true, true, Field,
26145b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
261582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
261682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
26178b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanianstatic void EncodeBitField(const ASTContext *Context, std::string& S,
2618153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                           const FieldDecl *FD) {
26198b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
26208b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
26218b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
26229a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
26238b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
26248b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
26258b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
26268b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
262782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
262882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
262982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
2630153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
2631090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
26326ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
2633e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const BuiltinType *BT = T->getAsBuiltinType()) {
2634ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
2635ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      return EncodeBitField(this, S, FD);
2636ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    char encoding;
2637ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    switch (BT->getKind()) {
2638ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    default: assert(0 && "Unhandled builtin type kind");
2639ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Void:       encoding = 'v'; break;
2640ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Bool:       encoding = 'B'; break;
2641ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_U:
2642ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UChar:      encoding = 'C'; break;
2643ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UShort:     encoding = 'S'; break;
2644ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt:       encoding = 'I'; break;
2645ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULong:
264672696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        encoding =
2647ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner          (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
264872696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        break;
2649ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt128:    encoding = 'T'; break;
2650ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULongLong:  encoding = 'Q'; break;
2651ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_S:
2652ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::SChar:      encoding = 'c'; break;
2653ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Short:      encoding = 's'; break;
2654ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int:        encoding = 'i'; break;
2655ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Long:
2656ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      encoding =
2657ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2658ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      break;
2659ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongLong:   encoding = 'q'; break;
2660ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int128:     encoding = 't'; break;
2661ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Float:      encoding = 'f'; break;
2662ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Double:     encoding = 'd'; break;
2663ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongDouble: encoding = 'd'; break;
266443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
2665ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2666ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += encoding;
2667ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2668ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2669ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2670ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ComplexType *CT = T->getAsComplexType()) {
2671c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
2672c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2673c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
2674ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2675ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2676ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
267735366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const PointerType *PT = T->getAsPointerType()) {
267885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    QualType PointeeTy = PT->getPointeeType();
2679a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
2680a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
2681a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
2682a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2683a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // Also, do not emit the 'r' for anything but the outermost type!
26848e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
2685a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
2686a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2687a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2688a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2689a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2690a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    else if (OutermostType) {
2691a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
269235366a67baa970c287c714c957cf78a4131cf60dTed Kremenek      while (P->getAsPointerType())
269335366a67baa970c287c714c957cf78a4131cf60dTed Kremenek        P = P->getAsPointerType()->getPointeeType();
2694a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
2695a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2696a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2697a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2698a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2699a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
2700a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
2701a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
2702a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
2703a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      const char * s = S.c_str();
2704a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      int len = S.length();
2705a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2706a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        std::string replace = "rn";
2707a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S.replace(S.end()-2, S.end(), replace);
2708a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2709a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
271014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (isObjCSelType(PointeeTy)) {
27118baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += ':';
27128baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
2713c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian    }
271485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
271585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
271685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
271785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
2718e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
271985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
272085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
272185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
27229533a7fdb8397421f3be52e879442460a87389f6Steve Naroff    } else if (const RecordType *RTy = PointeeTy->getAsRecordType()) {
27239533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
27249533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
27259533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
27269533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
27279533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
27289533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
27299533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
27309533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
27319533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
27329533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
27339533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
273485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
273585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
2736a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
2737a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
2738ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
273943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
2740ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2741ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2742ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2743ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
2744ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
2745ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
2746559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
2747559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
2748559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
2749559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
2750559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      getObjCEncodingForTypeImpl(AT->getElementType(), S,
2751559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
2752559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
2753559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
275485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2755559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2756559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
2757559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
2758559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
2759559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2760559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
2761559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
276285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2763559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      getObjCEncodingForTypeImpl(AT->getElementType(), S,
2764559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
2765559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
2766559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
2767ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2768ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2769ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2770ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->getAsFunctionType()) {
2771c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
2772ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2773ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2774ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
277535366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const RecordType *RTy = T->getAsRecordType()) {
277682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
2777d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
2778502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
2779502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2780502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
2781502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
2782502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
2783502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
27840d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
27857d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
278617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
278717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   FieldEnd = RDecl->field_end();
278844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
278943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
2790d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
279144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
2792d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
2793d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
2794d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar
2795d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
279643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
279743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
279843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
2799d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
2800a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
2801a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
2802a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getObjCEncodingForTypeImpl(qt, S, false, true,
280343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
2804d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
28057d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
28066de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
2807d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
2808ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2809ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2810ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2811ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
28128b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
28138b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
28148b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
28158b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
2816ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2817ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2818ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2819ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
282021a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
2821ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2822ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2823ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2824ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isObjCInterfaceType()) {
282543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
282643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
282743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
282843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
282943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
283043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
2831f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
283243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
2833f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
283443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
283543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
283643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
283743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
283843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
283943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
284043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
284143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
2842ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
284343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
2844ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2845ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) {
284614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
284714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
284814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
2849ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
2850ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2851ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCClassType()) {
285214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
285314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
2854ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
2855ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2856ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
285714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      getObjCEncodingForTypeImpl(getObjCIdType(), S,
285814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
285914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
286014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
286114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
286214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
286314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
286467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
286567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
286614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
286714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
286814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
286914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
287014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
287114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
287214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
2873ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
2874ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2875ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
2876ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
2877ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
2878ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
2879ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // We encode the underlying type which comes out as
2880ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
2881ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
2882ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      getObjCEncodingForTypeImpl(PointeeTy, S,
2883ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 false, ExpandPointedToStructures,
2884ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
288514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
288614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
2887ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2888ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
2889ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD || EncodingProperty) {
2890ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
289167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      S += OPT->getInterfaceDecl()->getNameAsCString();
289267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
289367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
2894ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
2895ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
2896ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
2897ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      }
2898ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
2899ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
2900ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2901ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
2902ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
2903ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
290485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
290585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2906a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
2907ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
2908ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
2909ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
2910ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
2911ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
2912ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
2913ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
2914ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
2915ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
2916ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
2917ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
2918ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
2919ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
2920ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
2921ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
2922ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
2923b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2924b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2925b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
2926b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
2927b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2928ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
2929de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
29307e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
29317e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
2932ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
2933319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCSelType = T;
2934319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2935319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const TypedefType *TT = T->getAsTypedefType();
2936319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (!TT)
2937319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    return;
2938319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  TypedefDecl *TD = TT->getDecl();
2939b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
2940b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  // typedef struct objc_selector *SEL;
294135366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2942c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!ptr)
2943c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
2944b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2945c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!rec)
2946c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
2947b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  SelStructType = rec;
2948b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
2949b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
2950ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
2951a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
2952390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
2953390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
2954ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
2955de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
29568baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
29578baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
2958a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2959a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCConstantStringType.isNull() &&
29602198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
29612198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
2962a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
29632198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
29642198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
29657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a qualified
29667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c std::vector.
29677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
29687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  bool TemplateKeyword,
29697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  TemplateDecl *Template) {
29707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
29717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
29727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
29737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
29747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName *QTN =
29757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
29767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (!QTN) {
29777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
29787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
29797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
29807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
29817532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
29827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
29837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
29847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
29857532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
29867532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
29877532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
29887532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  assert(NNS->isDependent() && "Nested name specifier must be dependent");
29897532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
29907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
29917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
29927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
29937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
29947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
29957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
29967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
29977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
29987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
29997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
30007532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
30017532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
30027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
30037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
30047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
30057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
30067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
30077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
30087532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
30097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
30107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
30117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3012b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
3013d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
3014d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
3015d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas GregorQualType ASTContext::getFromTargetType(unsigned Type) const {
3016b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
3017b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::NoInt: return QualType();
3018b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
3019b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
3020b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
3021b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
3022b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
3023b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
3024b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
3025b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3026b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
3027b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
3028b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
3029b3ac5434ab936f092b8cc48349cb01db3a1e1c76Daniel Dunbar  return QualType();
3030b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
3031b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3032b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3033b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
3034b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3035b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3036fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
3037fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
3038fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
3039f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
3040fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
3041fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
3042fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3043fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
304440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
3045fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
3046fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
3047fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  return false;
3048fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
3049fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
30504fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
30514fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
30524fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
30534fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz JahanianQualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3054b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttrs = QualType::GCNone;
30554fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
30564fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
3057b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
30584fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
3059a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // (or pointers to them) be treated as though they were declared
3060a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
3061a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    if (GCAttrs == QualType::GCNone) {
3062f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff      if (Ty->isObjCObjectPointerType())
3063a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian        GCAttrs = QualType::Strong;
3064a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
306535366a67baa970c287c714c957cf78a4131cf60dTed Kremenek        return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
3066a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
3067c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
3068c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
3069f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
3070c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian      return QualType::GCNone;
30714fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
3072b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
30734fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
30744fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
30756ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
30766ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
30776ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
3078770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
30796ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// areCompatVectorTypes - Return true if the two specified vector types are
30806ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
30816ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
30826ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
30836ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  assert(LHS->isCanonical() && RHS->isCanonical());
30846ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
308561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
30866ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
30876ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
30884084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
30894084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
30904084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
30914084c306635b70f37029dca938444e6013f08684Steve Naroff
30924084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
30934084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
30944084c306635b70f37029dca938444e6013f08684Steve Naroffstatic bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
30954084c306635b70f37029dca938444e6013f08684Steve Naroff                                           ObjCProtocolDecl *rProto) {
30964084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
309714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
30984084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
30994084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
31004084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
31014084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
31024084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
31034084c306635b70f37029dca938444e6013f08684Steve Naroff}
310414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
31054084c306635b70f37029dca938444e6013f08684Steve Naroff/// ClassImplementsProtocol - Checks that 'lProto' protocol
31064084c306635b70f37029dca938444e6013f08684Steve Naroff/// has been implemented in IDecl class, its super class or categories (if
31074084c306635b70f37029dca938444e6013f08684Steve Naroff/// lookupCategory is true).
31084084c306635b70f37029dca938444e6013f08684Steve Naroffstatic bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
31094084c306635b70f37029dca938444e6013f08684Steve Naroff                                    ObjCInterfaceDecl *IDecl,
31104084c306635b70f37029dca938444e6013f08684Steve Naroff                                    bool lookupCategory,
31114084c306635b70f37029dca938444e6013f08684Steve Naroff                                    bool RHSIsQualifiedID = false) {
31124084c306635b70f37029dca938444e6013f08684Steve Naroff
31134084c306635b70f37029dca938444e6013f08684Steve Naroff  // 1st, look up the class.
31144084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCList<ObjCProtocolDecl> &Protocols =
31154084c306635b70f37029dca938444e6013f08684Steve Naroff    IDecl->getReferencedProtocols();
31164084c306635b70f37029dca938444e6013f08684Steve Naroff
31174084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
31184084c306635b70f37029dca938444e6013f08684Steve Naroff       E = Protocols.end(); PI != E; ++PI) {
31194084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
31204084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
31214084c306635b70f37029dca938444e6013f08684Steve Naroff    // This is dubious and is added to be compatible with gcc.  In gcc, it is
31224084c306635b70f37029dca938444e6013f08684Steve Naroff    // also allowed assigning a protocol-qualified 'id' type to a LHS object
31234084c306635b70f37029dca938444e6013f08684Steve Naroff    // when protocol in qualified LHS is in list of protocols in the rhs 'id'
31244084c306635b70f37029dca938444e6013f08684Steve Naroff    // object. This IMO, should be a bug.
31254084c306635b70f37029dca938444e6013f08684Steve Naroff    // FIXME: Treat this as an extension, and flag this as an error when GCC
31264084c306635b70f37029dca938444e6013f08684Steve Naroff    // extensions are not enabled.
31274084c306635b70f37029dca938444e6013f08684Steve Naroff    if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
31284084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
31294084c306635b70f37029dca938444e6013f08684Steve Naroff  }
31304084c306635b70f37029dca938444e6013f08684Steve Naroff
31314084c306635b70f37029dca938444e6013f08684Steve Naroff  // 2nd, look up the category.
31324084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lookupCategory)
31334084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
31344084c306635b70f37029dca938444e6013f08684Steve Naroff         CDecl = CDecl->getNextClassCategory()) {
31354084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
31364084c306635b70f37029dca938444e6013f08684Steve Naroff           E = CDecl->protocol_end(); PI != E; ++PI)
31374084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lProto, *PI))
31384084c306635b70f37029dca938444e6013f08684Steve Naroff          return true;
31394084c306635b70f37029dca938444e6013f08684Steve Naroff    }
31404084c306635b70f37029dca938444e6013f08684Steve Naroff
31414084c306635b70f37029dca938444e6013f08684Steve Naroff  // 3rd, look up the super class(s)
31424084c306635b70f37029dca938444e6013f08684Steve Naroff  if (IDecl->getSuperClass())
31434084c306635b70f37029dca938444e6013f08684Steve Naroff    return
31444084c306635b70f37029dca938444e6013f08684Steve Naroff      ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
31454084c306635b70f37029dca938444e6013f08684Steve Naroff                              RHSIsQualifiedID);
31464084c306635b70f37029dca938444e6013f08684Steve Naroff
31474084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
31484084c306635b70f37029dca938444e6013f08684Steve Naroff}
31494084c306635b70f37029dca938444e6013f08684Steve Naroff
31504084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
31514084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
31524084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
31534084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
31544084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
31554084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
31564084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
31574084c306635b70f37029dca938444e6013f08684Steve Naroff}
31584084c306635b70f37029dca938444e6013f08684Steve Naroff
31594084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
31604084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
31614084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
31624084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
31634084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
31644084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isVoidPointerType() ||
31654084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
31664084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
31674084c306635b70f37029dca938444e6013f08684Steve Naroff  else if (rhs->isVoidPointerType() ||
31684084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
31694084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
31704084c306635b70f37029dca938444e6013f08684Steve Naroff
31714084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
31724084c306635b70f37029dca938444e6013f08684Steve Naroff    const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType();
31734084c306635b70f37029dca938444e6013f08684Steve Naroff
31744084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
31754084c306635b70f37029dca938444e6013f08684Steve Naroff
31764084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
31774084c306635b70f37029dca938444e6013f08684Steve Naroff      // If the RHS is a unqualified interface pointer "NSString*",
31784084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
31794084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
31804084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
31814084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
31824084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
31834084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
31844084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
31854084c306635b70f37029dca938444e6013f08684Steve Naroff          if (!ClassImplementsProtocol(*I, rhsID, true))
31864084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
31874084c306635b70f37029dca938444e6013f08684Steve Naroff        }
31884084c306635b70f37029dca938444e6013f08684Steve Naroff      }
31894084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
31904084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
31914084c306635b70f37029dca938444e6013f08684Steve Naroff    }
3192de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    // Both the right and left sides have qualifiers.
31934084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
31944084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
31954084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
31964084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
3197de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
3198de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
3199de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
3200de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
32014084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
32024084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
32034084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
32044084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
32054084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
32064084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
32078f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
32088f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
3209de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
32104084c306635b70f37029dca938444e6013f08684Steve Naroff      // If the RHS is a qualified interface pointer "NSString<P>*",
32114084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
32124084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
32134084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
32144084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
32154084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
32164084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
32174084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
32184084c306635b70f37029dca938444e6013f08684Steve Naroff          if (ClassImplementsProtocol(*I, rhsID, true)) {
32194084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
32204084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
32214084c306635b70f37029dca938444e6013f08684Steve Naroff          }
32224084c306635b70f37029dca938444e6013f08684Steve Naroff        }
32234084c306635b70f37029dca938444e6013f08684Steve Naroff      }
32244084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
3225de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
3226de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
32274084c306635b70f37029dca938444e6013f08684Steve Naroff
3228de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
3229de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
32304084c306635b70f37029dca938444e6013f08684Steve Naroff
32314084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
32324084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
32334084c306635b70f37029dca938444e6013f08684Steve Naroff
32344084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsOPT =
32354084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
32364084c306635b70f37029dca938444e6013f08684Steve Naroff    if (lhsOPT->qual_empty()) {
32374084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
32384084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
32394084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
32404084c306635b70f37029dca938444e6013f08684Steve Naroff             E = rhsQID->qual_end(); I != E; ++I) {
32414084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
32424084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
32434084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
32444084c306635b70f37029dca938444e6013f08684Steve Naroff          if (ClassImplementsProtocol(*I, lhsID, true)) {
32454084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
32464084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
32474084c306635b70f37029dca938444e6013f08684Steve Naroff          }
32484084c306635b70f37029dca938444e6013f08684Steve Naroff        }
32494084c306635b70f37029dca938444e6013f08684Steve Naroff        if (!match)
32504084c306635b70f37029dca938444e6013f08684Steve Naroff          return false;
32514084c306635b70f37029dca938444e6013f08684Steve Naroff      }
32524084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
32534084c306635b70f37029dca938444e6013f08684Steve Naroff    }
32544084c306635b70f37029dca938444e6013f08684Steve Naroff    // Both the right and left sides have qualifiers.
32554084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
32564084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
32574084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
32584084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
32594084c306635b70f37029dca938444e6013f08684Steve Naroff
32604084c306635b70f37029dca938444e6013f08684Steve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
32614084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
32624084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
32634084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
32644084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
32654084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
32664084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
32674084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
32684084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
32694084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
32704084c306635b70f37029dca938444e6013f08684Steve Naroff        }
32714084c306635b70f37029dca938444e6013f08684Steve Naroff      }
32724084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
32734084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
32744084c306635b70f37029dca938444e6013f08684Steve Naroff    }
32754084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
32764084c306635b70f37029dca938444e6013f08684Steve Naroff  }
32774084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
32784084c306635b70f37029dca938444e6013f08684Steve Naroff}
32794084c306635b70f37029dca938444e6013f08684Steve Naroff
32804084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
32814084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
32824084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
32834084c306635b70f37029dca938444e6013f08684Steve Naroff///
32844084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
32854084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
32864084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
32874084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
32884084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
32894084c306635b70f37029dca938444e6013f08684Steve Naroff
32904084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
32914084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
32924084c306635b70f37029dca938444e6013f08684Steve Naroff                                             QualType(RHSOPT,0),
32934084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
32944084c306635b70f37029dca938444e6013f08684Steve Naroff
32954084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
32964084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
32974084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHS && RHS) // We have 2 user-defined types.
32984084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
32994084c306635b70f37029dca938444e6013f08684Steve Naroff
33004084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
330114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
330214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
33033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
33043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman                                         const ObjCInterfaceType *RHS) {
33056ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
33066ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
33076ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
33086ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
33096ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
33106ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
33116ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
3312c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
33136ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
33146ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
33156ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
33166ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
3317c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (RHS->getNumProtocols() == 0)
33186ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
33196ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
3320c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3321c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                        LHSPE = LHS->qual_end();
332291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
332391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
332491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
332591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
332691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
3327c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
33284084c306635b70f37029dca938444e6013f08684Steve Naroff                                          RHSPE = RHS->qual_end();
33298f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
33308f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
333191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
33328f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
33338f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
333491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
333591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
333691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
333791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
333891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
333991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
334091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
33416ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
33426ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
3343389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3344389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
334514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType();
334614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType();
3347389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
334814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
3349389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
335014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
335114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
335214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
3353389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
3354389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
3355ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
3356ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
3357ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// C99 6.2.7p1: Two types have compatible types if their types are the
3358ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
33593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
33603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
33613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
33623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
33633d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
33643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *lbase = lhs->getAsFunctionType();
33653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *rbase = rhs->getAsFunctionType();
336672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
336772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
33683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
33693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
33703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
33713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
33723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
33733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
337461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
337561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
337661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
337761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
33786dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  // FIXME: double check this
33792455636163fdd18581d7fdae816433f886d88213Mike Stump  bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
33802455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != lbase->getNoReturnAttr())
33812455636163fdd18581d7fdae816433f886d88213Mike Stump    allLTypes = false;
33822455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != rbase->getNoReturnAttr())
33832455636163fdd18581d7fdae816433f886d88213Mike Stump    allRTypes = false;
33842455636163fdd18581d7fdae816433f886d88213Mike Stump
33853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
3386465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3387465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
33883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
33893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
33903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
33913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
33923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
33933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
33943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
33953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
33963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
33973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
33983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
33997fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
34007fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
34017fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
34023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
34033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
34043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
34053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
34063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
34073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argtype = mergeTypes(largtype, rargtype);
34083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
34093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
341061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
341161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
341261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
341361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
34143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
34153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
34163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
34173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
34182455636163fdd18581d7fdae816433f886d88213Mike Stump                           lproto->isVariadic(), lproto->getTypeQuals(),
34192455636163fdd18581d7fdae816433f886d88213Mike Stump                           NoReturn);
34203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
34213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
34223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
34233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
34243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
342572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
34263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
3427465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
34283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
34293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
34303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
34313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
34323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
34333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
34343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
34353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
34363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
34373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
34383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
34393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
34403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
34413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
34423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
34433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
34443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
34452d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getNumArgs(), proto->isVariadic(),
34462d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getTypeQuals(), NoReturn);
34473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
34483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
34493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
34503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
34512455636163fdd18581d7fdae816433f886d88213Mike Stump  return getFunctionNoProtoType(retType, NoReturn);
34523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
34533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
34543d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
345543d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
345643d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
345743d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
34587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
34597c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
34603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // FIXME: C++ shouldn't be going through here!  The rules are different
34613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // enough that they should be handled separately.
34627c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
34637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // shouldn't be going through here!
346435366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const ReferenceType *RT = LHS->getAsReferenceType())
3465c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    LHS = RT->getPointeeType();
346635366a67baa970c287c714c957cf78a4131cf60dTed Kremenek  if (const ReferenceType *RT = RHS->getAsReferenceType())
3467c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    RHS = RT->getPointeeType();
34683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
34693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
34703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
34713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3472f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
34733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
34743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
34753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
34763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // If the qualifiers are different, the types aren't compatible
34775a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // Note that we handle extended qualifiers later, in the
34785a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // case for ExtQualType.
34795a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
34803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
34813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3482852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
3483852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
3484f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
34851adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
34861adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
34871adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
34881adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
34894c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
349007d258756dc856c6987c394a0972884e6ed46765Eli Friedman  // Strip off objc_gc attributes off the top level so they can be merged.
349107d258756dc856c6987c394a0972884e6ed46765Eli Friedman  // This is a complete mess, but the attribute itself doesn't make much sense.
3492585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  if (RHSClass == Type::ExtQual) {
349307d258756dc856c6987c394a0972884e6ed46765Eli Friedman    QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
349407d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (GCAttr != QualType::GCNone) {
349586f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
34968df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      // __weak attribute must appear on both declarations.
349786f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // __strong attribue is redundant if other decl is an objective-c
349886f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // object pointer (or decorated with __strong attribute); otherwise
349986f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // issue error.
350086f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
350186f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian          (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
350214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff           !LHSCan->isObjCObjectPointerType()))
35038df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        return QualType();
35048df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian
350507d258756dc856c6987c394a0972884e6ed46765Eli Friedman      RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
350607d258756dc856c6987c394a0972884e6ed46765Eli Friedman                     RHS.getCVRQualifiers());
350707d258756dc856c6987c394a0972884e6ed46765Eli Friedman      QualType Result = mergeTypes(LHS, RHS);
35088df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      if (!Result.isNull()) {
35098df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        if (Result.getObjCGCAttr() == QualType::GCNone)
35108df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = getObjCGCQualType(Result, GCAttr);
35118df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        else if (Result.getObjCGCAttr() != GCAttr)
35128df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = QualType();
35138df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      }
351407d258756dc856c6987c394a0972884e6ed46765Eli Friedman      return Result;
351507d258756dc856c6987c394a0972884e6ed46765Eli Friedman    }
3516585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  }
3517585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  if (LHSClass == Type::ExtQual) {
351807d258756dc856c6987c394a0972884e6ed46765Eli Friedman    QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
351907d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (GCAttr != QualType::GCNone) {
35208df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
35218df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      // __weak attribute must appear on both declarations. __strong
352286f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // __strong attribue is redundant if other decl is an objective-c
352386f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // object pointer (or decorated with __strong attribute); otherwise
352486f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // issue error.
352586f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
352686f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian          (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
352714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff           !RHSCan->isObjCObjectPointerType()))
35288df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        return QualType();
352986f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian
353007d258756dc856c6987c394a0972884e6ed46765Eli Friedman      LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
353107d258756dc856c6987c394a0972884e6ed46765Eli Friedman                     LHS.getCVRQualifiers());
353207d258756dc856c6987c394a0972884e6ed46765Eli Friedman      QualType Result = mergeTypes(LHS, RHS);
35338df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      if (!Result.isNull()) {
35348df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        if (Result.getObjCGCAttr() == QualType::GCNone)
35358df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = getObjCGCQualType(Result, GCAttr);
35368df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        else if (Result.getObjCGCAttr() != GCAttr)
35378df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = QualType();
35388df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      }
3539354e53da656237e25518b3fafa3bf84de6e6f57eEli Friedman      return Result;
354007d258756dc856c6987c394a0972884e6ed46765Eli Friedman    }
3541585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  }
3542585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian
35434c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
3544a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3545a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
3546a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3547a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
3548a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner
3549213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
3550213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3551213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
3552ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
35534e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
35544e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
35551adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
35561adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // a signed integer type, or an unsigned integer type.
35573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = LHS->getAsEnumType()) {
35583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
35593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
3560bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
35613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = RHS->getAsEnumType()) {
35623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
35633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
3564bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
35651adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
35663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
3567ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
35683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
35694a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
35701adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
357172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
357272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
357372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
357472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
357572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
357672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
357772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
357872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
35797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
35807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
358172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
358272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
358372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
358472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
358572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
358672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
358772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
358872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
358972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
359072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
359172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
35921adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
35933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
35943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
359535366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
359635366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
35973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
35983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
359907d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
360061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
360107d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
360261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
36033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
36043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
3605c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
3606c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
3607c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
360835366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
360935366a67baa970c287c714c957cf78a4131cf60dTed Kremenek    QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3610c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3611c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
3612c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3613c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
3614c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3615c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
3616c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
3617c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
36181adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
36193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
36203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
36213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
36223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
36233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
36243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
36263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
36273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
36283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
362961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
363061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
363161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
363261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
36333bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
36343bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
36353bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
36363bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
36373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
36383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
363961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
364061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
364161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
364261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
36433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
36443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
36453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
36463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
36473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
36483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
36493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
36503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
36513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
36523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
36533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
36543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
36553bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
36563bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
36577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
36587e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
36593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
36601adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
36613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return mergeFunctionTypes(LHS, RHS);
366272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
366372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
36643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
36651adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
36663cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
36673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
366864cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
366964cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
367064cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
36713cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
36725a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
36733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
36743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
367561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
367661490e9a965cfee8a78c12c6802138844f04250dCedric Venet  case Type::ObjCInterface: {
36775fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    // Check if the interfaces are assignment compatible.
36785a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
36795a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
36805fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
36815fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
36825fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    if (LHSIface && RHSIface &&
36835fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff        canAssignObjCInterfaces(LHSIface, RHSIface))
36845fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
36855fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
36863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
368761490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
368814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
368914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(),
369014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                RHS->getAsObjCObjectPointerType()))
369114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
369214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
3693bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
369414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
36955a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::FixedWidthInt:
36965a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // Distinct fixed-width integers are not compatible.
36975a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
36985a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::ExtQual:
36995a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: ExtQual types can be compatible even if they're not
37005a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // identical!
37015a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
37025a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // First attempt at an implementation, but I'm not really sure it's
37035a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // right...
37045a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#if 0
37055a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* LQual = cast<ExtQualType>(LHSCan);
37065a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* RQual = cast<ExtQualType>(RHSCan);
37075a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
37085a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman        LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
37095a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return QualType();
37105a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
37115a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    LHSBase = QualType(LQual->getBaseType(), 0);
37125a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    RHSBase = QualType(RQual->getBaseType(), 0);
37135a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = mergeTypes(LHSBase, RHSBase);
37145a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (ResultType.isNull()) return QualType();
37155a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
37165a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LHSCan.getUnqualifiedType() == ResCanUnqual)
37175a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return LHS;
37185a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (RHSCan.getUnqualifiedType() == ResCanUnqual)
37195a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return RHS;
37205a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
37215a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
37225a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
37235a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return ResultType;
37245a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#endif
37257532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
37267532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  case Type::TemplateSpecialization:
37277532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    assert(false && "Dependent types have no size");
37287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
3729ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
373072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
373172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
3732ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
37337192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
37345426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
3735ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
3736ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
373788054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
3738ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
3739ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (T == BoolTy)
3740ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
3741f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3742f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth();
3743f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  }
3744f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
3745ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
3746ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
3747ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
3748ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3749ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
3750ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (const EnumType* ETy = T->getAsEnumType())
3751ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
3752ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  const BuiltinType* BTy = T->getAsBuiltinType();
3753ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert (BTy && "Unexpected signed integer type");
3754ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
3755ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
3756ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
3757ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
3758ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
3759ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
3760ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
3761ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
3762ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
3763ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
3764ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
3765ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
37662df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
37672df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
3768ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
3769ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
3770ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
3771ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
3772ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
3773ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
37742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
37752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
37762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
377786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
377886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
377986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
378086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
378186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
378286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
378386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
378486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters.  This returns the resultant type.
378586df27bbdbb98c39ec2184695c0561209f91beddChris Lattnerstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
378686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
378786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  bool AllowTypeModifiers = true) {
378886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
378986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
379086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
379186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
379286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the modifiers first.
379386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
379486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
379586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
379686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    default: Done = true; --Str; break;
379786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
379886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
379986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
380086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
380186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
380286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
380386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
380486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
380586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
380686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
380786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
380886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
380986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
381086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
381186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
381286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
381386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
381486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
381586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
381686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
381786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
381886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
381986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
382086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
382186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
382286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
382386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
382486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
382586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
382686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
382786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
382886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
382986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
383086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
383186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
383286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
383386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
383486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
383586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
383686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
383786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
383886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
383986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
384086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
384186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
384286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
384386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
384486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
384586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
384686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
384786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
384886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
384986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
385086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
385186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
385286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
385386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
385486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
385586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
385686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
385786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
385886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
385986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
386086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
386186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
386286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
386386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
386486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
386586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
386686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
386786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
386886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
386986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
387086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
387186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
387286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
387386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
387486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
387586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
387686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
387786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
387886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
387986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
388086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
388186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
388286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
388386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
388486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
388586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
388686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
388786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
388886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
388986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Type->isArrayType()) {
389086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
389186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    } else {
389286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
389386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
389486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
389586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
389686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
389786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
389886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
389986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
390086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
390186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
390286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
390386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
390486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getVectorType(ElementType, NumElements);
390586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
390686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
390786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'P': {
3908c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
3909c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
391086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Error = ASTContext::GE_Missing_FILE;
391186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
3912c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    } else {
3913c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor      break;
391486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
391586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
391686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
391786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
391886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (!AllowTypeModifiers)
391986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return Type;
392086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
392186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Done = false;
392286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
392386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
392486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      default: Done = true; --Str; break;
392586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '*':
392686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getPointerType(Type);
392786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
392886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '&':
392986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getLValueReferenceType(Type);
393086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
393186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      // FIXME: There's no way to have a built-in with an rvalue ref arg.
393286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case 'C':
393386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Type.getQualifiedType(QualType::Const);
393486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
393586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
393686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
393786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
393886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
393986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
394086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
394186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
394286df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id,
394386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                    GetBuiltinTypeError &Error) {
394486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(id);
394586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
394686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
394786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
394886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
394986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
395086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
395186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
395286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
395386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
395486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
395586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
395686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
395786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
395886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
395986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
396086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
396186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
396286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
396386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
396486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
396586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
396686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
396786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
396886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (ArgTypes.size() == 0 && TypeStr[0] == '.')
396986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return getFunctionNoProtoType(ResType);
397086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
397186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                         TypeStr[0] == '.', 0);
397286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
3973