ASTContext.cpp revision 1eb4433ac451dc16f4133a88af2d002ac26c58ef
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
17aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
18b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
19e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2119cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
221b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
23a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
2585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
266fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
27557c5b1717bc8919b1b40cf2064b51491ec53a44Chris Lattner#include "llvm/Support/MemoryBuffer.h"
2829445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson#include "RecordLayoutBuilder.h"
2929445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3661710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
3761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner                       TargetInfo &t,
38e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
391b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       bool FreeMem, unsigned size_reserve) :
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
42782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
470f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCIdRedefinitionType = QualType();
480f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCClassRedefinitionType = QualType();
491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
50e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
5114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
52e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
53e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Deallocate all the types.
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!Types.empty()) {
574b05b1dee6cc65ae61d93dab7edff72710f24589Ted Kremenek    Types.back()->Destroy(*this);
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Types.pop_back();
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
60b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
61b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
62b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
63b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
64b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
65b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
67b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
68b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
69b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
70b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
71b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar    llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
72b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar      I = ObjCLayouts.begin(), E = ObjCLayouts.end();
73b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
74b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
75b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
76b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
77b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
78b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
79ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
801ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
811ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         NNSEnd = NestedNameSpecifiers.end();
831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump       NNS != NNSEnd;
841ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor       /* Increment in loop */)
851ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
86ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
87ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
88ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
89ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
90b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
1017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
102dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
104dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
105dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
106dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
107dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
108c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
111dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
114dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
115dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
116dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
117dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
118dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
119dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
120dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
121dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
122dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
1231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
134f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
14615b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
16864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
1692df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
1702df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
1712df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
1722df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
1733a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
1743a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
1753a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
1763a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
17764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
178f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
179f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
180f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
181f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
182f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
183f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
184f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
185f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
186f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
187f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
1888e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
189898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
190898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
191898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
192898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
193898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
194898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
195898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
196898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
1978e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
1981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Placeholder type for C++0x auto declarations whose real type has
199e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // not yet been deduced.
200e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
2011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
2035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
2068e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2077e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
2081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
209de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
210de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
211de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
2121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Builtin types for 'id' and 'Class'.
214de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
215de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
21614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
217a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
22033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2216e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2226e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2236e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorVarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) {
2277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
2287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos
2297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
2307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
2317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
2347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) {
2387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
2397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
2407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
2417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
2427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatedFromStaticDataMember[Inst] = Tmpl;
2437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2450d8df780aef1acda5962347a32591efc629b6748Anders CarlssonUnresolvedUsingDecl *
2460d8df780aef1acda5962347a32591efc629b6748Anders CarlssonASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
2480d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    = InstantiatedFromUnresolvedUsingDecl.find(UUD);
2490d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
2500d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
2511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2520d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
2530d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
2540d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
2550d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
2560d8df780aef1acda5962347a32591efc629b6748Anders CarlssonASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
2570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                                   UnresolvedUsingDecl *UUD) {
2580d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
2590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson         "Already noted what using decl what instantiated from");
2600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
2610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
2620d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
263d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
264d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
265d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
266d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
267d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
2681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
269d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
270d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
271d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
272d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
273d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
274d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
275d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
276d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
277d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
279d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
280d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
281d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
2822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace {
2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  class BeforeInTranslationUnit
2842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    : std::binary_function<SourceRange, SourceRange, bool> {
2852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    SourceManager *SourceMgr;
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  public:
2882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
2891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    bool operator()(SourceRange X, SourceRange Y) {
2912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
2922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
2932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  };
2942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
2952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
2962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Determine whether the given comment is a Doxygen-style comment.
2972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
2982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Start the start of the comment text.
2992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
3002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param End the end of the comment text.
3012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
3022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Member whether we want to check whether this is a member comment
3032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
3042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// we only return true when we find a non-member comment.
3051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool
3061eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpisDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
3072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                 bool Member = false) {
3081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const char *BufferStart
3092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
3102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
3112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
3121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (End - Start < 4)
3142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
3152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  assert(Start[0] == '/' && "Not a comment?");
3172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
3182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
3192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
3202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
3212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return (Start[3] == '<') == Member;
3232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
3242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Retrieve the comment associated with the given declaration, if
3261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// it has one.
3272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorconst char *ASTContext::getCommentForDecl(const Decl *D) {
3282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!D)
3292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check whether we have cached a comment string for this declaration
3322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // already.
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::DenseMap<const Decl *, std::string>::iterator Pos
3342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = DeclComments.find(D);
3352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Pos != DeclComments.end())
3362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return Pos->second.c_str();
3372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we have an external AST source and have not yet loaded comments from
3392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // that source, do so now.
3402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (ExternalSource && !LoadedExternalComments) {
3412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::vector<SourceRange> LoadedComments;
3422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ExternalSource->ReadComments(LoadedComments);
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!LoadedComments.empty())
3452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      Comments.insert(Comments.begin(), LoadedComments.begin(),
3462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                      LoadedComments.end());
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    LoadedExternalComments = true;
3492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If there are no comments anywhere, we won't find anything.
3522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Comments.empty())
3532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the declaration doesn't map directly to a location in a file, we
3562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // can't find the comment.
3572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  SourceLocation DeclStartLoc = D->getLocStart();
3582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
3592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Find the comment that occurs just before this declaration.
3622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator LastComment
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = std::lower_bound(Comments.begin(), Comments.end(),
3642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       SourceRange(DeclStartLoc),
3652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       BeforeInTranslationUnit(&SourceMgr));
3661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the location for the start of the declaration and find the
3682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // beginning of the file buffer.
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<FileID, unsigned> DeclStartDecomp
3702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(DeclStartLoc);
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const char *FileBufferStart
3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(DeclStartDecomp.first).first;
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // First check whether we have a comment for a member.
3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment != Comments.end() &&
3762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
3772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      isDoxygenComment(SourceMgr, *LastComment, true)) {
3782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> LastCommentEndDecomp
3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(LastComment->getEnd());
3802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
3812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor        SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
3821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
3832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                                     LastCommentEndDecomp.second)) {
3842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // The Doxygen member comment comes after the declaration starts and
3852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is on the same line and in the same file as the declaration. This
3862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is the comment we want.
3872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      std::string &Result = DeclComments[D];
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.append(FileBufferStart +
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      SourceMgr.getFileOffset(LastComment->getBegin()),
3902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                    FileBufferStart + LastCommentEndDecomp.second + 1);
3912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return Result.c_str();
3922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
3932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment == Comments.begin())
3962e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  --LastComment;
3982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the end of the comment.
4002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::pair<FileID, unsigned> LastCommentEndDecomp
4012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(LastComment->getEnd());
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment and the declaration aren't in the same file, then they
4042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // aren't related.
4052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartDecomp.first != LastCommentEndDecomp.first)
4062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check that we actually have a Doxygen comment.
4092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!isDoxygenComment(SourceMgr, *LastComment))
4102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
4111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Compute the starting line for the declaration and for the end of the
4132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // comment (this is expensive).
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned DeclStartLine
4152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
4162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned CommentEndLine
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
4182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                              LastCommentEndDecomp.second);
4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment does not end on the line prior to the declaration, then
4212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // the comment is not associated with the declaration at all.
4222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (CommentEndLine + 1 != DeclStartLine)
4232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // We have a comment, but there may be more comments on the previous lines.
4262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Keep looking so long as the comments are still Doxygen comments and are
4272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // still adjacent.
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ExpectedLine
4292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
4302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator FirstComment = LastComment;
4312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != Comments.begin()) {
4322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Look at the previous comment
4332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    --FirstComment;
4342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> Decomp
4352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this previous comment is in a different file, we're done.
4382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Decomp.first != DeclStartDecomp.first) {
4392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this comment is not a Doxygen comment, we're done.
4442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!isDoxygenComment(SourceMgr, *FirstComment)) {
4452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If the line number is not what we expected, we're done.
4502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
4512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Line != ExpectedLine) {
4522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Set the next expected line number.
4571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ExpectedLine
4582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
4592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // The iterator range [FirstComment, LastComment] contains all of the
4622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // BCPL comments that, together, are associated with this declaration.
4632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Form a single comment block string for this declaration that concatenates
4642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // all of these comments.
4652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::string &Result = DeclComments[D];
4662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != LastComment) {
4672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompStart
4682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
4692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompEnd
4702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
4712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    Result.append(FileBufferStart + DecompStart.second,
4722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                  FileBufferStart + DecompEnd.second + 1);
4732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ++FirstComment;
4742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Append the last comment line.
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Result.append(FileBufferStart +
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                  SourceMgr.getFileOffset(LastComment->getBegin()),
4792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                FileBufferStart + LastCommentEndDecomp.second + 1);
4802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return Result.c_str();
4812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
4822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
483464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
484464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
485464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
486a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
487b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
488b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
489b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
490b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  const BuiltinType *BT = T->getAsBuiltinType();
491b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
492b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
493b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
494b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
495b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
496b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
497b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
498b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
499b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
500af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// getDeclAlign - Return a conservative estimate of the alignment of the
501af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
502af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
503b7d0844c887a72064b624dc6df12cbe1441f69d0Daniel Dunbarunsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
504dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
505dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
50640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
507dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    Align = std::max(Align, AA->getAlignment());
508dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
509af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
510af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
5116217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
5124cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      unsigned AS = RT->getPointeeType().getAddressSpace();
513f0930235ce58a91aa3b840bece9052f44d630536Anders Carlsson      Align = Target.getPointerAlign(AS);
5144cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson    } else if (!T->isIncompleteType() && !T->isFunctionType()) {
5154cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
516dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
517dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
518dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
519dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
520dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
521af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
522dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
523dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  return Align / Target.getCharWidth();
524af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
525b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
526a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
527a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
528d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
5291d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
5305e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
5315e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
532a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
53372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
53472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
53518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
53672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
53772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
53818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
53972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
54072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5415d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
5425d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
54318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
54418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
54518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
54618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
54718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
54872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
549fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
55018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
55118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
55218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
55318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5547e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithExpr:
5557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithoutExpr:
556fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
5571d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55998be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
5609e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
561030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
562030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
5635c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
564213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
565030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
56798be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<VectorType>(T)->getElementType());
5689e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
5694bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
5706fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
5716fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
5726fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // FIXME: this should probably be a target property.
5736fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    Align = 1 << llvm::Log2_32_Ceil(Align);
574030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
575030d8846c7e520330007087e949f621989876e3aChris Lattner  }
5765d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
5779e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
578a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
579692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
580d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
58118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
58218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
58318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
58418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
58518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5866f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
5879e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
5889e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
5896f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
590692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
591692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
592692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
5936f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
5949e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
5959e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
5966f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
59764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
59864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
59964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
60064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
601f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
602f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
603f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
604f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
605f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
606f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
607f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
608f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
609692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
6106f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
6119e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
6129e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
6136f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
614692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
6156f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
6169e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
6179e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
6186f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
619692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
6206f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
6219e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
6229e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
6236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
624692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
6256f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
6269e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
6279e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
6286f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
629ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
630ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
631ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
632ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
633ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
6346f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
6359e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
6369e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
6376f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6386f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
6395426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
6405426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
6416f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6426f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
6439e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
6449e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
6456f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6466e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
6476e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
6486e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
6491590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
650a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
651bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
652f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  case Type::FixedWidthInt:
653f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // FIXME: This isn't precisely correct; the width/alignment should depend
654f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // on the available types for the target
655f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Width = cast<FixedWidthIntType>(T)->getWidth();
656736166b38235cf6d0ffb67638960d95fb2afcbd6Chris Lattner    Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
657f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Align = Width;
658f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    break;
659f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  case Type::ExtQual:
66098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // FIXME: Pointers into different addr spaces could have different sizes and
66198be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // alignment requirements: getPointerInfo should take an AddrSpace.
662f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
663d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
6645426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
665f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
6666f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
667485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
668485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
669485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
670485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
671485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
672485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
673f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
674f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
6755426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
676f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
677f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
678f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
6797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
6807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
6817ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner    // "When applied to a reference or a reference type, the result is the size
6825d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // of the referenced type." C++98 5.3.3p2: expr.sizeof.
6836f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // FIXME: This is wrong for struct layout: a reference in a struct has
6846f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // pointer size.
685bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
686f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
6871cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
6881cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
6891cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // If we ever want to support other ABIs this needs to be abstracted.
6901cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson
691f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
6931cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
6941cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
695f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
696f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
6971cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
6981cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
699f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
7005d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
7015d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
7025d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
7031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
70498be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
7059e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
7065d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
7075d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
7085d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
70944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
7101d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
71144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
71244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
71344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
71444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
71544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
71672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
71772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
7181d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
7191d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
7201d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
7218389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
7228389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
7238389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
7248389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7261d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
7277176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
7287176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
7291d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
7307176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
7317176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
7327176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
733dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
734a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
7357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
7367da2431c23ef1ee8acb114e39692246e1801afc2John McCall  case Type::Elaborated: {
7377da2431c23ef1ee8acb114e39692246e1801afc2John McCall    return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
7387da2431c23ef1ee8acb114e39692246e1801afc2John McCall  }
7397da2431c23ef1ee8acb114e39692246e1801afc2John McCall
74018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
74118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
74240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
74318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = Aligned->getAlignment();
74418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
74518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
74618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
7477532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
7487176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
74918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
75018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
75118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
75218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
75318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
75418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
75518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
75618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
757395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
758395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
759395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
760395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
76118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::QualifiedName:
76218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
76618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
76718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
76818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
76918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
77018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
77118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
773464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
7749e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
775a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
776a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
77734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
77834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
77934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
78034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
78134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
78234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
7831eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
7841eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
7851eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (const ComplexType* CT = T->getAsComplexType())
7861eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
7871eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
7881eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
7891eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
7901eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
79134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
79234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
79334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
794a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
795a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
796a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
797a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
798a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
799f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
800a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
801a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
802a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
803a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
804a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
805a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
806a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
807a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
808a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
809a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
810a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
811a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
8128e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
8138e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
8148e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
8158e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
8168e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
8178e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 bool CollectSynthesized) {
8188e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
8198e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian         E = OI->ivar_end(); I != E; ++I) {
8208e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian     Ivars.push_back(*I);
8218e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8228e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  if (CollectSynthesized)
8238e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    CollectSynthesizedIvars(OI, Ivars);
8248e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8258e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8269820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
8279820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
82817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
82917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
8309820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
8319820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8339820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into nested protocols.
8349820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
8359820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
8369820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(*P, Ivars);
8379820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
8389820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
8399820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// CollectSynthesizedIvars -
8409820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// This routine collect synthesized ivars for the designated class.
8419820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
8429820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
8439820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
84417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
84517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8469820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
8479820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
8489820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8499820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into interface's protocol list for properties declared
8509820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // in the protocol and whose ivars are synthesized.
8519820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8529820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8539820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8549820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(PD, Ivars);
8559820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8569820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
8579820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
8588e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
8598e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
86017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
86117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
8628e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8638e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8648e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8658e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into nested protocols.
8668e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
8678e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
8688e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(*P);
8698e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8708e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8718e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpunsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
8738e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
87417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
87517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8768e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8778e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8788e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8798e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into interface's protocol list for properties declared
8808e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // in the protocol and whose ivars are synthesized.
8818e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8828e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8838e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8848e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(PD);
8858e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8868e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8878e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8888e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8898a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
8908a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
8918a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8928a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
8938a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
8948a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
8958a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
8968a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
8978a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
8988a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
8998a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
9008a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
9018a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
9028a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
9038a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
9048a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
9058a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
9068a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
9078a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
9088a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
9098a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
9108a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
9118a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
9128a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
9138a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
9148a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
9158a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
9168a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
9178a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
9188a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
919b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Allocate an uninitialized DeclaratorInfo.
920b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
921b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// The caller should initialize the memory held by DeclaratorInfo using
922b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
923b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
924b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
925b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
926b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
927b17166c8077cd900cca83a895c43b30ea6660598Argyrios KyrtzidisDeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
928b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
929b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  DeclaratorInfo *DInfo =
930b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis    (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
931b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  new (DInfo) DeclaratorInfo(T);
932b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  return DInfo;
933b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
934b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
935b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the
936b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface.
937b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar///
938b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's
939b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars.
94044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout &
941b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
942b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar                          const ObjCImplementationDecl *Impl) {
943532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar  assert(!D->isForwardDecl() && "Invalid interface decl!");
944532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar
94544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Look up this layout, if already laid out, return what we have.
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ObjCContainerDecl *Key =
947d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
948d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
949d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    return *Entry;
95044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
951453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // Add in synthesized ivar count if laying out an implementation.
952453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
95329445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    unsigned FieldCount = D->ivar_size();
9548e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    unsigned SynthCount = CountSynthesizedIvars(D);
9558e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    FieldCount += SynthCount;
956d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    // If there aren't any sythesized ivars then reuse the interface
957453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entry. Note we can't cache this because we simply free all
958453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entries later; however we shouldn't look up implementations
959453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // frequently.
9608e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if (SynthCount == 0)
961453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar      return getObjCLayout(D, 0);
962453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  }
963453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar
9641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
96529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
96629445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  ObjCLayouts[Key] = NewEntry;
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  return *NewEntry;
96944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel}
97044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
971b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
972b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
973b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
974b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
975b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
976b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
977b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
978b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
979b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
980b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
98188a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
982464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
983464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
98498be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
9854b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  D = D->getDefinition(*this);
9864b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  assert(D && "Cannot get layout of forward declarations!");
9874bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
988464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
989ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // Note that we can't save a reference to the entry because this function
990ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // is recursive.
991ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  const ASTRecordLayout *Entry = ASTRecordLayouts[D];
992464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
9934bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
9941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
99529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D);
996ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  ASTRecordLayouts[D] = NewEntry;
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9985d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
999464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
1000464175bba1318bef7905122e9fda20cff926df78Chris Lattner
1001a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1002a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1003a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1004a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1005f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
1006f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1007f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1008f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1009b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
1010b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
1011b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
1012b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
1013b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttr = QualType::GCNone;
1014b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
10151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1016b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
1017b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // If this type already has an address space specified, it cannot get
1018b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
1019b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getAddressSpace() == 0 &&
1020b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner           "Type cannot be in multiple addr spaces!");
1021b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttr = EQT->getObjCGCAttr();
1022b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
1023b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1025b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // Check if we've already instantiated this type.
1026ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  llvm::FoldingSetNodeID ID;
10272455636163fdd18581d7fdae816433f886d88213Mike Stump  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
1028ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  void *InsertPos = 0;
1029f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
1030b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
1031b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
1032ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // If the base type isn't canonical, this won't be a canonical type either,
1033ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // so fill in the canonical type field.
1034ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  QualType Canonical;
1035b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (!TypeNode->isCanonical()) {
1036f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    Canonical = getAddrSpaceQualType(CanT, AddressSpace);
10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1038b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
1039f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1040f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1041ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  }
1042b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
1043b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
1044f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
1045ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  Types.push_back(New);
1046b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
1047ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1048ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1049b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
1050b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner                                       QualType::GCAttrTypes GCAttr) {
1051d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1052b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1053d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10554027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  if (T->isPointerType()) {
10566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
105758f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
10584027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
10594027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
10604027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
10614027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
1062b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
1063b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
1064b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
1065b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
1066b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned AddressSpace = 0;
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1068b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
10692455636163fdd18581d7fdae816433f886d88213Mike Stump    // If this type already has an ObjCGC specified, it cannot get
1070b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
1071b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getObjCGCAttr() == QualType::GCNone &&
10722455636163fdd18581d7fdae816433f886d88213Mike Stump           "Type cannot have multiple ObjCGCs!");
1073b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    AddressSpace = EQT->getAddressSpace();
1074b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
1075b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1077d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check if we've already instantiated an gc qual'd type of this type.
1078d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  llvm::FoldingSetNodeID ID;
10792455636163fdd18581d7fdae816433f886d88213Mike Stump  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
1080d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  void *InsertPos = 0;
1081d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
1082b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
10831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1084d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // If the base type isn't canonical, this won't be a canonical type either,
1085d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // so fill in the canonical type field.
10865a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // FIXME: Isn't this also not canonical if the base type is a array
10875a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // or pointer type?  I can't find any documentation for objc_gc, though...
1088d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType Canonical;
1089d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (!T->isCanonical()) {
1090b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    Canonical = getObjCGCQualType(CanT, GCAttr);
10911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1092b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
1093d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1094d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1095d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
1096b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
1097b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
1098d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
1099d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  Types.push_back(New);
1100b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
1101d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1102a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
11032455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getNoReturnType(QualType T) {
11046dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  QualifierSet qs;
11056dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  qs.strip(T);
11062455636163fdd18581d7fdae816433f886d88213Mike Stump  if (T->isPointerType()) {
11076217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
11082455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType ResultType = getNoReturnType(Pointee);
11096dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getPointerType(ResultType);
11106dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType.setCVRQualifiers(T.getCVRQualifiers());
11116dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    return qs.apply(ResultType, *this);
11122455636163fdd18581d7fdae816433f886d88213Mike Stump  }
11132455636163fdd18581d7fdae816433f886d88213Mike Stump  if (T->isBlockPointerType()) {
11146217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
11152455636163fdd18581d7fdae816433f886d88213Mike Stump    QualType ResultType = getNoReturnType(Pointee);
11166dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getBlockPointerType(ResultType);
11176dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType.setCVRQualifiers(T.getCVRQualifiers());
11186dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    return qs.apply(ResultType, *this);
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
11202455636163fdd18581d7fdae816433f886d88213Mike Stump  if (!T->isFunctionType())
11212455636163fdd18581d7fdae816433f886d88213Mike Stump    assert(0 && "can't noreturn qualify non-pointer to function or block type");
11221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1123e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump  if (const FunctionNoProtoType *F = T->getAsFunctionNoProtoType()) {
11242455636163fdd18581d7fdae816433f886d88213Mike Stump    return getFunctionNoProtoType(F->getResultType(), true);
11252455636163fdd18581d7fdae816433f886d88213Mike Stump  }
1126e24aea225ec87b935ede6c21c964dd47a4afb810Mike Stump  const FunctionProtoType *F = T->getAsFunctionProtoType();
11272455636163fdd18581d7fdae816433f886d88213Mike Stump  return getFunctionType(F->getResultType(), F->arg_type_begin(),
11282455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
11292455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
11302455636163fdd18581d7fdae816433f886d88213Mike Stump                         F->getNumExceptions(), F->exception_begin(), true);
11312455636163fdd18581d7fdae816433f886d88213Mike Stump}
11322455636163fdd18581d7fdae816433f886d88213Mike Stump
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1149f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1153f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1155f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ComplexType *New = new (*this,8) ComplexType(T, Canonical);
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1161f98aba35e6c3da5aae61843fc01334939e4e12ecEli FriedmanQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1162f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1163f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman     SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1164f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  FixedWidthIntType *&Entry = Map[Width];
1165f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (!Entry)
1166f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Entry = new FixedWidthIntType(Width, Signed);
1167f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  return QualType(Entry, 0);
1168f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman}
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(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  PointerType::Profile(ID, T);
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
11811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1186f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
11871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1190f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1192f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  PointerType *New = new (*this,8) PointerType(T, Canonical);
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
11995618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
12005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
1201296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1202296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
12035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
12045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
12055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
12061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12075618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
12085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
12095618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
12105618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
12135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
12145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
12155618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (!T->isCanonical()) {
12165618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12185618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
12195618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
12205618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1221f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12225618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
1223f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
12245618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
12255618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
12265618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
12275618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
12285618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
12297c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
12307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
12317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getLValueReferenceType(QualType T) {
12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceType::Profile(ID, T);
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
12387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
12397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
12417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
12467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getLValueReferenceType(getCanonicalType(T));
12477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
12497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
12507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
12517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
12537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
12557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
12567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
12577c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
12587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
12597c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
12617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
12627c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
12637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
12647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
12657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
12667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  ReferenceType::Profile(ID, T);
12677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12687c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
12697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
12707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12717c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
12727c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12737c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
12747c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
12757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
12767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (!T->isCanonical()) {
12777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getRValueReferenceType(getCanonicalType(T));
12787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
12817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1282f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12857c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
12885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1291f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1292f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
12931eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
1294f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1295f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1296f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1297f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1298f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1299f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1300f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1301f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1302f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1303f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1304f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1305f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1306f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
1307f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (!T->isCanonical()) {
1308f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1309f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1310f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1311f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1312f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1313f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1314f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
1315f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
1316f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1317f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1318f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1319f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1320f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1322fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
132438aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1325c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1326c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1327587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman  assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1328587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1329587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
133038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
133138aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
133238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
133338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
13360be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
13371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
13407192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
13421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!EltTy->isCanonical()) {
13471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1348c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
13495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
13517192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1352f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1355566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  ConstantArrayType *New =
1356f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
13577192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithExprType - Return a reference to the type for
13637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithExprType(QualType EltTy,
13667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         const llvm::APInt &ArySizeIn,
13677e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         Expr *ArySizeExpr,
13687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         ArrayType::ArraySizeModifier ASM,
13697e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         unsigned EltTypeQuals,
13707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         SourceRange Brackets) {
13717e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13727e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13737e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13757e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13767e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
13777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
13787e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
13797e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Since we don't unique expressions, it isn't possible to unique VLA's
13807e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // that have an expression provided for their size.
13817e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithExprType *New =
13827e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
13837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ArySize, ArySizeExpr,
13847e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ASM, EltTypeQuals, Brackets);
13857e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
13867e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
13877e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
13887e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithoutExprType - Return a reference to the type for
13907e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13917e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithoutExprType(QualType EltTy,
13937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            const llvm::APInt &ArySizeIn,
13947e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArrayType::ArraySizeModifier ASM,
13957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            unsigned EltTypeQuals) {
13967e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13977e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13987e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13997e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
14007e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
14017e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
14027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
14037e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
14047e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithoutExprType *New =
14057e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
14067e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                             ArySize, ASM, EltTypeQuals);
14077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
14087e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
14097e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
14107e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
1411bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1412bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
14137e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
14147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1415c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
14167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          unsigned EltTypeQuals,
14177e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          SourceRange Brackets) {
1418c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1419c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1420c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1421566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  VariableArrayType *New =
14227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)VariableArrayType(EltTy, QualType(),
14237e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  NumElts, ASM, EltTypeQuals, Brackets);
1424c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1425c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1426c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1427c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1428c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1429c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1430898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1431898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
143204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
14337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy,
14347e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                Expr *NumElts,
1435898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
14367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                unsigned EltTypeQuals,
14377e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                SourceRange Brackets) {
14381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1439898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1440898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
144104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  llvm::FoldingSetNodeID ID;
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
144304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                   EltTypeQuals, NumElts);
1444898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
144504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  void *InsertPos = 0;
144604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *Canon
144704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
144804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *New;
144904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  if (Canon) {
145004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // We already have a canonical version of this array type; use it as
145104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // the canonical type for a newly-built type.
14521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    New = new (*this,8) DependentSizedArrayType(*this, EltTy,
145304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                QualType(Canon, 0),
145404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                NumElts, ASM, EltTypeQuals,
145504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                Brackets);
145604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  } else {
145704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    QualType CanonEltTy = getCanonicalType(EltTy);
145804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    if (CanonEltTy == EltTy) {
145904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      New = new (*this,8) DependentSizedArrayType(*this, EltTy, QualType(),
146004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  NumElts, ASM, EltTypeQuals,
146104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  Brackets);
146204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      DependentSizedArrayTypes.InsertNode(New, InsertPos);
146304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    } else {
146404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
146504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  ASM, EltTypeQuals,
146604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  SourceRange());
146704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      New = new (*this,8) DependentSizedArrayType(*this, EltTy, Canon,
146804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  NumElts, ASM, EltTypeQuals,
14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                  Brackets);
147004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    }
147104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  }
14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1473898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1474898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1475898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1476898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1477c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1478c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1479c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1480c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
14810be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1482c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1483c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (IncompleteArrayType *ATP =
1485c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1486c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1487c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1488c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1489c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1490c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1491c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1492c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (!EltTy->isCanonical()) {
1493f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
14942bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1495c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1496c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1497c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1498c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1499f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15002bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1501c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
15027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  IncompleteArrayType *New
15037e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    = new (*this,8) IncompleteArrayType(EltTy, Canonical,
15047e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                        ASM, EltTypeQuals);
1505c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1506c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1507c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1508c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1509fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1510fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
151173322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
151273322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
151373322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1516f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
151773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  VectorType::Profile(ID, vecType, NumElts, Type::Vector);
15225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
15285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
15295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!vecType->isCanonical()) {
1530f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getVectorType(getCanonicalType(vecType), NumElts);
15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1534f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1536f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
15385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1542213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
154373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1544213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
154573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
15461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1547f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1548213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
15491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
155173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
155373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
155473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
155573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
155673322924127c873c13101b705dd823f5539ffa5fSteve Naroff
155773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
155873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
155973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
156073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (!vecType->isCanonical()) {
1561213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
15621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
156373322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
156473322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1565f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
156673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
1567f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
156873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
156973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
157073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
157173322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
157273322924127c873c13101b705dd823f5539ffa5fSteve Naroff
15731eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
15749cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    Expr *SizeExpr,
15759cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    SourceLocation AttrLoc) {
15762ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
15771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
15782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
15791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15802ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
15812ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
15822ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
15832ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
15842ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
15852ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
15862ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
15872ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
15882ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                    QualType(Canon, 0),
15892ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                    SizeExpr, AttrLoc);
15902ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
15912ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
15922ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
15931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                      QualType(), SizeExpr,
15952ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      AttrLoc);
15962ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
15972ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
15982ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
15992ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
16002ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      New = new (*this,8) DependentSizedExtVectorType(*this, vecType, Canon,
16012ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SizeExpr, AttrLoc);
16022ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
16032ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
16041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
16069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
16079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
16089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
160972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
16112455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
16152455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
161972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!ResultTy->isCanonical()) {
16242455636163fdd18581d7fdae816433f886d88213Mike Stump    Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
16251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
162772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
162872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1629f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16322455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType *New
16332455636163fdd18581d7fdae816433f886d88213Mike Stump    = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn);
16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
163572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
16365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
16405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
164161710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1642971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1643465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     unsigned TypeQuals, bool hasExceptionSpec,
1644465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     bool hasAnyExceptionSpec, unsigned NumExs,
16452455636163fdd18581d7fdae816433f886d88213Mike Stump                                     const QualType *ExArray, bool NoReturn) {
16465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
16475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
164972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1650465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                             TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
16512455636163fdd18581d7fdae816433f886d88213Mike Stump                             NumExs, ExArray, NoReturn);
16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
165572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1657465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1658465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
16595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCanonical = ResultTy->isCanonical();
1660465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  if (hasExceptionSpec)
1661465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    isCanonical = false;
16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
16635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!ArgArray[i]->isCanonical())
16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
16655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
1667465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
16695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isCanonical) {
16705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
16715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
16725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
1673f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner      CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
1674465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1675f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1676beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
167747259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                isVariadic, TypeQuals, false,
167847259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                false, 0, 0, NoReturn);
1679465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
16805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
168172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
168272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1683f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1685465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
168672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1687465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
1688465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // end of them.
16891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionProtoType *FTP =
1690465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1691465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumArgs*sizeof(QualType) +
1692465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumExs*sizeof(QualType), 8);
169372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1694465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                              TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
16952455636163fdd18581d7fdae816433f886d88213Mike Stump                              ExArray, NumExs, Canonical, NoReturn);
16965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
169772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
16985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17012ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
17022ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
17034b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
17041e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
17052ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17071e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
17082ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1709fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  else if (isa<TemplateTypeParmDecl>(Decl)) {
1710fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    assert(false && "Template type parameter types are always available.");
17119fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else if (ObjCInterfaceDecl *ObjCInterface
17129fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump               = dyn_cast<ObjCInterfaceDecl>(Decl))
17132ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getObjCInterfaceType(ObjCInterface);
171449aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1715c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1716566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1717566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1718f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1719f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) RecordType(Record);
17209fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1721566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1722566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1723f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1724f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) EnumType(Enum);
17259fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
17262ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    assert(false && "TypeDecl without a type?");
172749aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
17284b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
172949aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
17302ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
17312ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) {
17355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
17361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1737f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
173872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
17415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1743fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
17441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
174576e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
17461eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
174776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
1748fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1749fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
175076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
1751fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
17521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
1753fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1754fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1755fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1756fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
17571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
175876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  if (Name) {
175976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
176076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
176176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                                   Name, Canon);
176276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
176376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
1764fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1765fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1766fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1767fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1768fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1769fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1770fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
17711eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
17727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
17737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
17747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
17757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          QualType Canon) {
1776b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  if (!Canon.isNull())
1777b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    Canon = getCanonicalType(Canon);
1778b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  else {
1779b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    // Build the canonical template specialization type.
17801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateName CanonTemplate = getCanonicalTemplateName(Template);
17811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::SmallVector<TemplateArgument, 4> CanonArgs;
17821275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    CanonArgs.reserve(NumArgs);
17831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
17841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
17851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
17861275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // Determine whether this canonical template specialization type already
17871275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // exists.
17881275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::FoldingSetNodeID ID;
17891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TemplateSpecializationType::Profile(ID, CanonTemplate,
1790828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                        CanonArgs.data(), NumArgs, *this);
17911275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
17921275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    void *InsertPos = 0;
17931275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateSpecializationType *Spec
17941275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
17951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17961275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    if (!Spec) {
17971275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // Allocate a new canonical template specialization type.
17981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      void *Mem = Allocate((sizeof(TemplateSpecializationType) +
17991275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                            sizeof(TemplateArgument) * NumArgs),
18001275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                           8);
18011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
18021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                                  CanonArgs.data(), NumArgs,
1803b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                                                  Canon);
18041275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Types.push_back(Spec);
18051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
18061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
18071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1808b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    if (Canon.isNull())
1809b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor      Canon = QualType(Spec, 0);
18101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(Canon->isDependentType() &&
18111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           "Non-dependent template-id type must have a canonical type");
1812b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  }
1813fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
18141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
18151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
18161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
18171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
181840808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
181940808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                       8);
18201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
18211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
1822828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                           Canon);
18231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
18251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
182655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
182755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
18281eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1829ab452ba8323d1985e08bade2bced588cddf2cc28Douglas GregorASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
1830e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor                                 QualType NamedType) {
1831e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1832ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  QualifiedNameType::Profile(ID, NNS, NamedType);
1833e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1834e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
18351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualifiedNameType *T
1836e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1837e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1838e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1839e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
18401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  T = new (*this) QualifiedNameType(NNS, NamedType,
1841ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor                                    getCanonicalType(NamedType));
1842e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1843e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameTypes.InsertNode(T, InsertPos);
1844e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1845e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1846e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
18471eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1848d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     const IdentifierInfo *Name,
1849d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     QualType Canon) {
1850d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1851d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1852d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1853d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1854d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (CanonNNS != NNS)
1855d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Canon = getTypenameType(CanonNNS, Name);
1856d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1857d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1858d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
1859d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType::Profile(ID, NNS, Name);
1860d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1861d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
18621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TypenameType *T
1863d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1864d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1865d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1866d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1867d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  T = new (*this) TypenameType(NNS, Name, Canon);
1868d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
1869d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
18701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
1871d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1872d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
18731eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
18741eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpASTContext::getTypenameType(NestedNameSpecifier *NNS,
18751734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            const TemplateSpecializationType *TemplateId,
18761734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            QualType Canon) {
18771734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
18781734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18791734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
18801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
18811734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
18821734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
18831734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
18841734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        = CanonType->getAsTemplateSpecializationType();
18851734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
18861734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
18871734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Canon = getTypenameType(CanonNNS, CanonTemplateId);
18881734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
18891734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
18901734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  llvm::FoldingSetNodeID ID;
18921734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType::Profile(ID, NNS, TemplateId);
18931734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18941734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  void *InsertPos = 0;
18951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TypenameType *T
18961734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
18971734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (T)
18981734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    return QualType(T, 0);
18991734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
19001734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  T = new (*this) TypenameType(NNS, TemplateId, Canon);
19011734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
19021734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
19031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
19041734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
19051734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
19067da2431c23ef1ee8acb114e39692246e1801afc2John McCallQualType
19077da2431c23ef1ee8acb114e39692246e1801afc2John McCallASTContext::getElaboratedType(QualType UnderlyingType,
19087da2431c23ef1ee8acb114e39692246e1801afc2John McCall                              ElaboratedType::TagKind Tag) {
19097da2431c23ef1ee8acb114e39692246e1801afc2John McCall  llvm::FoldingSetNodeID ID;
19107da2431c23ef1ee8acb114e39692246e1801afc2John McCall  ElaboratedType::Profile(ID, UnderlyingType, Tag);
19111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19127da2431c23ef1ee8acb114e39692246e1801afc2John McCall  void *InsertPos = 0;
19137da2431c23ef1ee8acb114e39692246e1801afc2John McCall  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
19147da2431c23ef1ee8acb114e39692246e1801afc2John McCall  if (T)
19157da2431c23ef1ee8acb114e39692246e1801afc2John McCall    return QualType(T, 0);
19167da2431c23ef1ee8acb114e39692246e1801afc2John McCall
19177da2431c23ef1ee8acb114e39692246e1801afc2John McCall  QualType Canon = getCanonicalType(UnderlyingType);
19187da2431c23ef1ee8acb114e39692246e1801afc2John McCall
19197da2431c23ef1ee8acb114e39692246e1801afc2John McCall  T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
19207da2431c23ef1ee8acb114e39692246e1801afc2John McCall  Types.push_back(T);
19217da2431c23ef1ee8acb114e39692246e1801afc2John McCall  ElaboratedTypes.InsertNode(T, InsertPos);
19227da2431c23ef1ee8acb114e39692246e1801afc2John McCall  return QualType(T, 0);
19237da2431c23ef1ee8acb114e39692246e1801afc2John McCall}
19247da2431c23ef1ee8acb114e39692246e1801afc2John McCall
192588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
192688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
192788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
192888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
19292e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
193088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
193188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
193288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
193388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
193488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
193688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
193788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
193888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
193988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
194088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
194188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
194288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
194388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1944d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1945d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// the given interface decl and the conforming protocol list.
194614108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
19471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              ObjCProtocolDecl **Protocols,
1948d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff                                              unsigned NumProtocols) {
1949d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // Sort the protocol list alphabetically to canonicalize it.
1950d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (NumProtocols)
1951d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
1952d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1953d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  llvm::FoldingSetNodeID ID;
195414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
1955d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1956d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
1957d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (ObjCObjectPointerType *QT =
1958d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1959d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    return QualType(QT, 0);
1960d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1961d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // No Match;
1962d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerType *QType =
196314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
19641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1965d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  Types.push_back(QType);
1966d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1967d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  return QualType(QType, 0);
1968d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
196988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1970c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// getObjCInterfaceType - Return the unique reference to the type for the
1971c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// specified ObjC interface decl. The list of protocols is optional.
1972c15cb2af27514ecc879daba9aa01389c5203685dSteve NaroffQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
1973a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                       ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
19741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (NumProtocols)
1975c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    // Sort the protocol list alphabetically to canonicalize it.
1976c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19784b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
1979c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19814b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
1982c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (ObjCInterfaceType *QT =
1983c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff      ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
19844b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
19851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19864b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  // No Match;
1987c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType *QType =
19881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1989c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                    Protocols, NumProtocols);
19904b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  Types.push_back(QType);
1991c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceTypes.InsertNode(QType, InsertPos);
19924b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  return QualType(QType, 0);
19934b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian}
19944b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
199572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
199672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
19979752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
19981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
19999752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
200072564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
2001dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2002b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2003b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2004b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
20051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2006b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2007b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2008b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2009b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2010b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2011b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
20121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      toe = new (*this, 8) TypeOfExprType(tofExpr,
2013b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
2014b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2015b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
2016b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
2017b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr);
2018b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2019b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2020b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2021b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2022dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
2023dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
2024dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
20259752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
20269752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2027d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2028d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
20299752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
20309752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
20319752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
20321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
20339752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
2034d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
2035f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
2036f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
20379752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
20389752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2039d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2040d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
204160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
204260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
204360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
2044a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2045a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
20461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
204760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
204860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
204960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
205060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
205160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
205260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
205360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
205460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
205560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
205660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
205760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
205860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
205960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
206060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
206160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
20621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
206360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
20641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
206660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
206760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (e->isLvalue(Context) == Expr::LV_Valid)
206860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
20691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
207060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
207160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
207260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2073395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2074395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2075395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
20761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2077395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
2078395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) {
2079dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
20809d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  if (e->isTypeDependent()) {
20819d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
20829d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
20831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20849d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
20859d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
20869d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
20879d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
20889d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
20899d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
20909d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = new (*this, 8) DecltypeType(e, DependentTy,
20919d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
20929d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
20939d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
20949d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
20959d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      Canon = new (*this, 8) DependentDecltypeType(*this, e);
20969d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
20979d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
20989d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
20999d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2100dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
21011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
2102dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2103395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2104395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2105395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2106395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
2109e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) {
2110d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2111e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2112e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2113e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
21171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
21195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const {
2120b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
212364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
212464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
212564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
212664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
212764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
212864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
212964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
213064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
213164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
213264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
213364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
213464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
213564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
213664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
21378b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
21388b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
21398b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2140b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
21418b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
21428b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2143e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2144e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2145e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2146e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
214777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
214877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
214977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
215077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
215177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
215277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
215350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) {
215477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner  QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
21551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2156c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the result has type qualifiers, make sure to canonicalize them as well.
2157c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (TypeQuals == 0)
215950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType);
2160c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2161c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the type qualifiers are on an array type, get the canonical type of the
2162c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // array with the qualifiers applied to the element type.
2163c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
2164c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
216550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType.getQualifiedType(TypeQuals));
21661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2167c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2168c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
2169c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
2170c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
21711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2172c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
217350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
217450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getConstantArrayType(NewEltTy, CAT->getSize(),
217550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getSizeModifier(),
217650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getIndexTypeQualifier()));
2177c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
217850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
217950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
218050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                    IAT->getIndexTypeQualifier()));
21811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2182898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
218350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
218450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getDependentSizedArrayType(NewEltTy,
2185bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                        DSAT->getSizeExpr() ?
2186bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                          DSAT->getSizeExpr()->Retain() : 0,
218750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getSizeModifier(),
218850d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getIndexTypeQualifier(),
218950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getBracketsRange()));
2190898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2191c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
219250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
2193bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                        VAT->getSizeExpr() ?
2194bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr()->Retain() : 0,
219550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                        VAT->getSizeModifier(),
219650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                  VAT->getIndexTypeQualifier(),
219750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                     VAT->getBracketsRange()));
2198c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
2199c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
220025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
220125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
220225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
220325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
220497fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
220525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
22061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If this template name refers to a set of overloaded function templates,
2207d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  /// the canonical template name merely stores the set of function templates.
22086ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor  if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
22096ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    OverloadedFunctionDecl *CanonOvl = 0;
22106ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
22116ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                                                FEnd = Ovl->function_end();
22126ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor         F != FEnd; ++F) {
22136ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      Decl *Canon = F->get()->getCanonicalDecl();
22146ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      if (CanonOvl || Canon != F->get()) {
22156ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor        if (!CanonOvl)
22161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          CanonOvl = OverloadedFunctionDecl::Create(*this,
22171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Ovl->getDeclContext(),
22186ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                                                    Ovl->getDeclName());
22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22206ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor        CanonOvl->addOverload(
22216ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                    AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
22226ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      }
22236ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    }
22241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22256ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    return TemplateName(CanonOvl? CanonOvl : Ovl);
22266ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor  }
22271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
222825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
222925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
223025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
223125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
223225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
22331eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
22341275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
22351275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
22361275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
22371275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
22381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22391275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
22401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Build canonical expression?
22411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
22421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22431275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
22441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
22451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              Arg.getAsDecl()->getCanonicalDecl());
22461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22471275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
22481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
22491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              *Arg.getAsIntegral(),
22501275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
22511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22521275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
22531275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
22541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getAsType()));
22551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22561275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
22571275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Allocate in ASTContext
22581275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
22591275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
22601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
22611275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
22621275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
22631275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
22641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22651275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument Result;
22661275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
22671275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Result;
22681275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
22691275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
22701275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
22711275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
22721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
22731275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
22741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
22751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
2276d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
2277d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
22781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
2279d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
2280d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2281d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
2282d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
2283d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
22841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
2285d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2286d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
2287d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2288d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
2289d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
2290d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
2291d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2292d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2293d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
2294d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
2295d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
22961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this, 0,
22971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2298d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
2299d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2300d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2301d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
2302d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
2303d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
2304d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2305d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2306d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
2307d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
2308d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2309d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2310c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2311c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
2312c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
2313c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (T.getCVRQualifiers() == 0) {
2314c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
2315c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2316c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
2317c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
23181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2319c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the common negative case fast, ignoring CVR qualifiers.
2320c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
23211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2322f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Make sure to look through type qualifiers (like ExtQuals) for the negative
2323c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // test.
2324c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!isa<ArrayType>(CType) &&
2325c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      !isa<ArrayType>(CType.getUnqualifiedType()))
2326c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
23271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2328c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Apply any CVR qualifiers from the array type to the element type.  This
2329c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
2330c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
23311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2332c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
2333c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
233450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
2335c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
2336c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned AddrSpace = 0;
2337c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  Type *Ty = T.getTypePtr();
23381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2339f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Rip through ExtQualType's and typedefs to get to a concrete type.
2340c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  while (1) {
2341f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
2342f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      AddrSpace = EXTQT->getAddressSpace();
2343f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      Ty = EXTQT->getBaseType();
2344c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    } else {
2345c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      T = Ty->getDesugaredType();
2346c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
2347c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner        break;
2348c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      CVRQuals |= T.getCVRQualifiers();
2349c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      Ty = T.getTypePtr();
2350c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    }
2351c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
23521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2353c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
2354c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2355c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
2356c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
23571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2358c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
2359c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
2360c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2361c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
2362c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy = ATy->getElementType();
2363c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (AddrSpace)
2364f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
2365c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
23661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2367c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2368c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2369c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
2370c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getIndexTypeQualifier()));
2371c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2372c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2373c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
23747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                  IAT->getIndexTypeQualifier()));
2375898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
23761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
2377898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
2378898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
23791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
2380bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                DSAT->getSizeExpr() ?
2381bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              DSAT->getSizeExpr()->Retain() : 0,
2382898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
23837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getIndexTypeQualifier(),
23847e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
23851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2386c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
23877e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
2388bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr() ?
2389bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                               VAT->getSizeExpr()->Retain() : 0,
2390c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
23917e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getIndexTypeQualifier(),
23927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
239377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
239477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
239577c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
2396e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
2397e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
2398e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
2399e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
2400e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
2401e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2402e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
2403c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
2404c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
2405c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
2406c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
2407c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2408c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
24091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2410c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
2411e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2412e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
2413c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
2414e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
2415e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
24165e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) {
24175e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  QualifierSet qualifiers;
24185e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  while (true) {
24195e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    const Type *UT = qualifiers.strip(QT);
24205e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
24215e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor      QT = AT->getElementType();
24226dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    } else {
24235e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor      return qualifiers.apply(QT, *this);
24245e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    }
24255e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  }
24265e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
24275e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
2428d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel DunbarQualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
24296183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  QualType ElemTy = VAT->getElementType();
24301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24316183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
24326183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson    return getBaseElementType(VAT);
24331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24346183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
24356183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
24366183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
24370de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
24381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
24390de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
24400de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
24410de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
24420de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
24430de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
24440de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
24450de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
24460de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
24470de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
24485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
24495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
2450a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
2451ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  if (const ComplexType *CT = T->getAsComplexType())
24525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
2453a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2454d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel Dunbar  assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
2455ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  switch (T->getAsBuiltinType()->getKind()) {
2456a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
24575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
24585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
24595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
24605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
24615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
24641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
2465716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
2466716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
24671361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
24681361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
24691361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
24701361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
24711361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
2472716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
2473f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
2474f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
2475f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
2476f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
2477f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
24781361b11066239ea15764a2a844405352d87296b3Chris Lattner
24791361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
24801361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
24811361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
24821361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
24831361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
24841361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
24855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
24865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
24897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
24907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
24911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
2492a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2493a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
2494a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
24951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2496a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
2497fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
2498a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
2499fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
2500fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
25015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
25025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2503f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2504f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
2505f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
2506f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
2507f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  assert(T->isCanonical() && "T should be canonicalized");
2508f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
2509f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    T = ET->getDecl()->getIntegerType().getTypePtr();
2510f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2511a34267595534a72703290153a6f7e3da1adcec59Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::WChar))
2512a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
2513a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
2514f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
2515f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2516f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2517f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
2518f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2519f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2520f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // There are two things which impact the integer rank: the width, and
2521f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // the ordering of builtins.  The builtin ordering is encoded in the
2522f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // bottom three bits; the width is encoded in the bits above that.
25231b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
2524f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth() << 3;
2525f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2526f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
25277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
25287cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
2529f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
25307cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
25317cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
25327cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
25337cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
2534f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
25357cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
25367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
2537f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
25387cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
25397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
2540f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
25417cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
25427cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
2543f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
25447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
25457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
2546f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
25472df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
25482df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
25492df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
2550f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
2551f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
2552f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
255304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
255404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
255504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
255604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
255704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
255804e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) {
255904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
256004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
256104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
256204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
256304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
256404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
256504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
256604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
256704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
256804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
256904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
257004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
257104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
257204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
257304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
257404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
257504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
257604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
257704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
257804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
257904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
258004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
258104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
258204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
258304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
2584a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
2585a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2586a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
2587a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2588a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
2589a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
2590a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
2591a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
2592a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
2593a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
2594a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2595a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2596a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
2597a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
25981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
25997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
26001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
26017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
2602f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
2603f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
26047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
26051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2606f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2607f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
26081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
26107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
26111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
26137cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
26147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
26157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
26161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
26187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
26197cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
26207cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
26217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
26221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
26247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
26251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
26267cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
26277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
26287cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
26297cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
26307cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
26317cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
26321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26337cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
26347cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
26351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
26367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
26375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
263871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
26391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
264071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
264171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
26421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
26431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2644df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek                         &Idents.get("NSConstantString"));
2645f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
26461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
26481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
2649f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2650f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
265171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
26521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
265371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
26541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
26551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
265644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
265744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
26581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
265944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
2660a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           FieldTypes[i], /*DInfo=*/0,
26611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
26624afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
266317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
266444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
266544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
266644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    CFConstantStringTypeDecl->completeDefinition(*this);
266771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
26681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
266971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
26708467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2671b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2672319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
26736217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2674319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2675319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2676319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2677319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
26781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getObjCFastEnumerationStateType() {
2679bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
268044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
268144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
268244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                         &Idents.get("__objcFastEnumerationState"));
26831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2684bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2685bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2686de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
2687bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2688bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2689bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2690bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
26911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
269244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
26931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this,
26941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           ObjCFastEnumerationStateTypeDecl,
26951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           SourceLocation(), 0,
2696a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           FieldTypes[i], /*DInfo=*/0,
26971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
26984afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
269917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
270044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
27011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
270244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
2703bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
27041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2705bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2706bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2707bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2708319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
27096217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2710319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2711319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2712319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2713319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2714e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
2715e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
27162d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
2717e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
2718bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2719bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
27201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
272185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
272285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
272385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2724a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
272533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
2726a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) {
272798be4943e8dc4f3905629a7102668960873cf863Chris Lattner  uint64_t sz = getTypeSize(type);
27281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
272933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
273033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (sz > 0 && type->isIntegralType())
273198be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = std::max(sz, getTypeSize(IntTy));
273233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
273333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
273498be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = getTypeSize(VoidPtrTy);
273598be4943e8dc4f3905629a7102668960873cf863Chris Lattner  return sz / getTypeSize(CharTy);
273633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
273733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2738a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
273933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
27401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
2741e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
2742c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2743ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
2744a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
274533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
27460d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
274733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
274833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
274933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
275033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
275198be4943e8dc4f3905629a7102668960873cf863Chris Lattner  int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
275233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
275333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
275433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  int ParmOffset = 2 * PtrSize;
275589951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
275689951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
275789951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
275889951a86b594513c2a013532ed45d197413b1087Chris Lattner    int sz = getObjCEncodingTypeSize(PType);
2759a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
276033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
276133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
276233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(ParmOffset);
276333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
276433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(PtrSize);
27651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
276733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
276889951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
276989951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
277089951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
27711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
27724306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
2773ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2774ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
2775ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
2776bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
2777ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
2778ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
2779ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
2780ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
278133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
27824306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
27830d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
278433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    S += llvm::utostr(ParmOffset);
2785a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ParmOffset += getObjCEncodingTypeSize(PType);
278633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
278733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
278833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2789c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
279083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
2791c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2792c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
27931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
27941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
27951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
27961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
27971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
279883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
279983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
280083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
280183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
280283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
280383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
280483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
280583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
280683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
280783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
280883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
280983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
281083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
281183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
281283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
281383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
28141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2815c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
2816e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
2817c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
2818c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
2819c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
2820c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2821c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
2822c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
28231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
2824c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
2825c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
282617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
2827653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2828c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2829c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2830c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2831c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2832c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2833c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2834c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2835c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2836c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2837c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
283861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
2839c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
284017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
2841653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2842c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2843c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2844c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2845c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2846c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2847c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2848c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2849c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
28501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2851c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2852c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2853c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2854c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2855c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
2856c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2857c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
2858090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
2859090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
28601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
2861090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
2862090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
2863c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2864c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
2865c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
2866c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
2867c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
2868c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
2869c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
28701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
2871c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2872c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2873c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2874c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
2875c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
2876c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
2877c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
2878c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2879090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2880090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
28811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2882c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2883c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
2884077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
2885c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2886c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2887c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2888c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
2889077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
2890c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2891c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2892c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
2893c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2894c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
289539f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
2896c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2897c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2898c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
2899c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
2900c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2901a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
29021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
29031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
2904a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2905a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
2906a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
29078e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
2908a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
2909c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
2910c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2911a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
29121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
2913c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
2914c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2915a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
2916a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2917a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
2918a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
2919a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
29207d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
2921153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
292282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
292382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
292482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
292582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
29261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
29275b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
292882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
292982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
29301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void EncodeBitField(const ASTContext *Context, std::string& S,
2931153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                           const FieldDecl *FD) {
29328b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
29338b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
29348b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
29359a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
29368b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
29378b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
29388b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
29398b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
294082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
294182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
294282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
2943153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
2944090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
29456ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
2946e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const BuiltinType *BT = T->getAsBuiltinType()) {
2947ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
2948ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      return EncodeBitField(this, S, FD);
2949ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    char encoding;
2950ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    switch (BT->getKind()) {
29511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: assert(0 && "Unhandled builtin type kind");
2952ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Void:       encoding = 'v'; break;
2953ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Bool:       encoding = 'B'; break;
2954ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_U:
2955ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UChar:      encoding = 'C'; break;
2956ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UShort:     encoding = 'S'; break;
2957ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt:       encoding = 'I'; break;
29581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::ULong:
29591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        encoding =
29601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
296172696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        break;
2962ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt128:    encoding = 'T'; break;
2963ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULongLong:  encoding = 'Q'; break;
2964ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_S:
2965ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::SChar:      encoding = 'c'; break;
2966ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Short:      encoding = 's'; break;
2967ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int:        encoding = 'i'; break;
29681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::Long:
29691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      encoding =
29701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2971ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      break;
2972ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongLong:   encoding = 'q'; break;
2973ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int128:     encoding = 't'; break;
2974ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Float:      encoding = 'f'; break;
2975ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Double:     encoding = 'd'; break;
2976ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongDouble: encoding = 'd'; break;
297743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
29781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2979ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += encoding;
2980ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2981ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
29821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2983ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ComplexType *CT = T->getAsComplexType()) {
2984c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
29851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2986c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
2987ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2988ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
29891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29906217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
299185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    QualType PointeeTy = PT->getPointeeType();
2992a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
2993a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
2994a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
2995a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
29961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
29978e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
2998a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
2999a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3000a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3001a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
30029fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
3003a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
30046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
30056217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
3006a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
3007a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
3008a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
3009a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
3010a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
3011a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
3012a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
3013a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
3014a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
3015a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      const char * s = S.c_str();
3016a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      int len = S.length();
3017a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3018a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        std::string replace = "rn";
3019a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S.replace(S.end()-2, S.end(), replace);
3020a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
3021a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
302214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (isObjCSelType(PointeeTy)) {
30238baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += ':';
30248baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
3025c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian    }
30261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
302785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
302885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
302985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
3030e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
303185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
303285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
303385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
30346217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
30359533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
30369533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
30379533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
30389533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
30399533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
30409533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
30419533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
30429533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
30439533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
30449533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
30459533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
304685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
304785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
3048a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
3049a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
30501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
305143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
3052ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3053ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
30541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3055ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
3056ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
3057ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
3058559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
3059559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
3060559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
3061559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
30621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3063559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3064559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
3065559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
30661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3067559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3068559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
3069559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
3070559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
3071559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3072559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
3073559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
30741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3076559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3077559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
3078559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
3079ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3080ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
30811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3082ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->getAsFunctionType()) {
3083c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
3084ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3085ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
30861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30876217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
308882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
3089d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
3090502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
3091502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3092502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
3093502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
3094502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
3095502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
30960d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
30977d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
309817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
309917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   FieldEnd = RDecl->field_end();
310044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
310143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
3102d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
310344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
3104d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
3105d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
31061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3107d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
310843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
31091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
311043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
3111d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
3112a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
3113a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
31141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(qt, S, false, true,
311543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
3116d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
31177d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
31186de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
3119d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
3120ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3121ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
31221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3123ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
31248b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
31258b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
31268b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
31278b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
3128ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3129ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
31301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3131ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
313221a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
3133ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3134ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
31351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3136ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isObjCInterfaceType()) {
313743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
313843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
313943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
314043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
314143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
314243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
3143f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
314443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
3145f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
314643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
31471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
314843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
314943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
31501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
315143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
315243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
315343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
3154ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
315543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
31561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3157ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) {
315814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
315914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
316014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3161ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
31621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3163ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCClassType()) {
316414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
316514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3166ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
31671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3168ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
31691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
317014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
317114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
317214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
317314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
317414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
317514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
317667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
317767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
317814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
317914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
318014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
318114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
318214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
318314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
318414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3185ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
31861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3187ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
3188ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
3189ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
3190ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
31911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
3192ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
3193ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
31941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
31951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
3196ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
319714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
319814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
3199ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3200ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
3201ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD || EncodingProperty) {
3202ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
320367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      S += OPT->getInterfaceDecl()->getNameAsCString();
320467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
320567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
3206ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
3207ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
3208ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
32091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3210ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
3211ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3212ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3213ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
32141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3215ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
321685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
321785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
32181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
3219ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
3220ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
3221ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
3222ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
3223ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
3224ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
3225ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
3226ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
3227ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
3228ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
3229ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
3230ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
3231ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
3232ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
3233ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
3234ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
3235b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
32361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3237b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
3238b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
3239b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3240ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
3241de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
32427e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
32437e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
3244ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
3245319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCSelType = T;
3246319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
3247319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const TypedefType *TT = T->getAsTypedefType();
3248319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (!TT)
3249319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    return;
3250319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  TypedefDecl *TD = TT->getDecl();
3251b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3252b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  // typedef struct objc_selector *SEL;
32536217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
3254c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!ptr)
3255c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
3256b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
3257c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!rec)
3258c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
3259b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  SelStructType = rec;
3260b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
3261b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3262ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
3263a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
3264390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
3265390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3266ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
3267de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
32688baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
32698baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
3270a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
32711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
32722198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
32731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3274a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
32752198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
32762198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
32777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a qualified
32787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c std::vector.
32791eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
32807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  bool TemplateKeyword,
32817532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  TemplateDecl *Template) {
32827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
32837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
32847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32857532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
32867532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName *QTN =
32877532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
32887532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (!QTN) {
32897532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
32907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
32917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
32927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
32947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
32957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3296d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
3297d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
32981eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
3299d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                                  bool TemplateKeyword,
3300d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                            OverloadedFunctionDecl *Template) {
3301d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
3302d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
33031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3304d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
3305d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
3306d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3307d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
3308d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3309d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3310d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
33111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3312d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
3313d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
3314d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
33157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
33167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
33171eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
33187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
33191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
33203b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
33217532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33227532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
33237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
33247532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33257532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
33267532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
33277532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
33287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33297532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
33307532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
33317532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33327532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
33337532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
33347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
33357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
33367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
33377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
33387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
33397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33407532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
33417532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
33427532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
33437532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3344b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
3345d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
3346d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
3347d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas GregorQualType ASTContext::getFromTargetType(unsigned Type) const {
3348b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
33491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TargetInfo::NoInt: return QualType();
3350b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
3351b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
3352b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
3353b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
3354b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
3355b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
3356b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
3357b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3358b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
3359b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
3360b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
3361b3ac5434ab936f092b8cc48349cb01db3a1e1c76Daniel Dunbar  return QualType();
3362b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
3363b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3364b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3365b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
3366b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3367b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3368fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
3369fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
3370fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
3371f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
3372fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
3373fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
3374fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3375fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
337640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
3377fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
3378fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
33791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
3380fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
3381fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
33824fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
33834fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
33844fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
33854fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz JahanianQualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3386b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttrs = QualType::GCNone;
33874fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
33884fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
3389b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
33904fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
33911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // (or pointers to them) be treated as though they were declared
3392a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
3393a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    if (GCAttrs == QualType::GCNone) {
3394f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff      if (Ty->isObjCObjectPointerType())
3395a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian        GCAttrs = QualType::Strong;
3396a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
33976217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
3398a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
3399c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
3400c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
3401f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
3402c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian      return QualType::GCNone;
34034fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
3404b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
34054fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
34064fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
34076ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
34086ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
34096ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
3410770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
34111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
34126ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
34136ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
34146ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
34156ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  assert(LHS->isCanonical() && RHS->isCanonical());
34166ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
341761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
34186ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
34196ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
34204084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
34214084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
34224084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
34234084c306635b70f37029dca938444e6013f08684Steve Naroff
34244084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
34254084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
34260fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
34270fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian                                                ObjCProtocolDecl *rProto) {
34284084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
342914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
34304084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
34314084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
34324084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
34334084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
34344084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
34354084c306635b70f37029dca938444e6013f08684Steve Naroff}
343614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
34374084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
34384084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
34394084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
34404084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
34414084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
34424084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
34434084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
34444084c306635b70f37029dca938444e6013f08684Steve Naroff}
34454084c306635b70f37029dca938444e6013f08684Steve Naroff
34464084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
34474084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
34484084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
34494084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
34504084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
34511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
34524084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
34534084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
34541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
34554084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
34564084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
34574084c306635b70f37029dca938444e6013f08684Steve Naroff
34584084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
34594084c306635b70f37029dca938444e6013f08684Steve Naroff    const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType();
34601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34614084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
34621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34634084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
34641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
34654084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
34664084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
34674084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34684084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
34694084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
34704084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
34714084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
34720fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
34734084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
34744084c306635b70f37029dca938444e6013f08684Steve Naroff        }
34754084c306635b70f37029dca938444e6013f08684Steve Naroff      }
34764084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
34774084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
34784084c306635b70f37029dca938444e6013f08684Steve Naroff    }
34791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
34804084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34814084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
34824084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
34834084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
3484de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
3485de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
3486de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
3487de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
34884084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
34894084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
34904084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
34914084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
34924084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
34934084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
34948f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
34958f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
3496de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
34971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
34984084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
34994084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
35004084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
35014084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
35024084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
35034084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
35044084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
35050fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
35064084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
35074084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
35084084c306635b70f37029dca938444e6013f08684Steve Naroff          }
35094084c306635b70f37029dca938444e6013f08684Steve Naroff        }
35104084c306635b70f37029dca938444e6013f08684Steve Naroff      }
35114084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
3512de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
3513de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
35141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3515de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
3516de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
35171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35184084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
35194084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
35204084c306635b70f37029dca938444e6013f08684Steve Naroff
35211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
35224084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
35234084c306635b70f37029dca938444e6013f08684Steve Naroff    if (lhsOPT->qual_empty()) {
35244084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
35254084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
35264084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
35274084c306635b70f37029dca938444e6013f08684Steve Naroff             E = rhsQID->qual_end(); I != E; ++I) {
35284084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
35294084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
35304084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
35310fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (lhsID->ClassImplementsProtocol(*I, true)) {
35324084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
35334084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
35344084c306635b70f37029dca938444e6013f08684Steve Naroff          }
35354084c306635b70f37029dca938444e6013f08684Steve Naroff        }
35364084c306635b70f37029dca938444e6013f08684Steve Naroff        if (!match)
35374084c306635b70f37029dca938444e6013f08684Steve Naroff          return false;
35384084c306635b70f37029dca938444e6013f08684Steve Naroff      }
35394084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
35404084c306635b70f37029dca938444e6013f08684Steve Naroff    }
35411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
35424084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
35434084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
35444084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
35454084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
35464084c306635b70f37029dca938444e6013f08684Steve Naroff
35474084c306635b70f37029dca938444e6013f08684Steve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
35484084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
35494084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
35504084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
35514084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
35524084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
35534084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
35544084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
35554084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
35564084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
35574084c306635b70f37029dca938444e6013f08684Steve Naroff        }
35584084c306635b70f37029dca938444e6013f08684Steve Naroff      }
35594084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
35604084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
35614084c306635b70f37029dca938444e6013f08684Steve Naroff    }
35624084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
35634084c306635b70f37029dca938444e6013f08684Steve Naroff  }
35644084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
35654084c306635b70f37029dca938444e6013f08684Steve Naroff}
35664084c306635b70f37029dca938444e6013f08684Steve Naroff
35674084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
35684084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
35694084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
35704084c306635b70f37029dca938444e6013f08684Steve Naroff///
35714084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
35724084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
35734084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
35744084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
35754084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
35764084c306635b70f37029dca938444e6013f08684Steve Naroff
35774084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
35781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
35791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
35804084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
35814084c306635b70f37029dca938444e6013f08684Steve Naroff
35824084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
35834084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
35844084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHS && RHS) // We have 2 user-defined types.
35854084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
35861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35874084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
358814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
358914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
35903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
35913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman                                         const ObjCInterfaceType *RHS) {
35926ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
35936ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
35946ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
35956ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
35961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35976ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
35986ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
3599c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
36006ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
36011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36026ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
36036ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
3604c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (RHS->getNumProtocols() == 0)
36056ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
36061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3607c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3608c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                        LHSPE = LHS->qual_end();
360991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
361091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
361191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
361291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
361391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
3614c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
36154084c306635b70f37029dca938444e6013f08684Steve Naroff                                          RHSPE = RHS->qual_end();
36168f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
36178f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
361891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
36198f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
36208f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
362191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
362291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
362391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
362491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
362591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
362691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
362791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
36286ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
36296ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
3630389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3631389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
363214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType();
363314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType();
36341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
363514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
3636389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
363714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
363814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
363914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
3640389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
3641389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
36421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
3643ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
36441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
3645ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
36463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
36473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
36483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
36493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36503d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
36513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *lbase = lhs->getAsFunctionType();
36523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *rbase = rhs->getAsFunctionType();
365372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
365472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
36553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
36563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
36573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
36593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
36603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
366161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
366261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
366361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
366461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
36656dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  // FIXME: double check this
36662455636163fdd18581d7fdae816433f886d88213Mike Stump  bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
36672455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != lbase->getNoReturnAttr())
36682455636163fdd18581d7fdae816433f886d88213Mike Stump    allLTypes = false;
36692455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != rbase->getNoReturnAttr())
36702455636163fdd18581d7fdae816433f886d88213Mike Stump    allRTypes = false;
36711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
3673465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3674465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
36753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
36763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
36773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
36793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
36803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
36813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
36833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
36843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
36853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36867fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
36877fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
36887fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
36893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
36903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
36913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
36923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
36933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
36943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argtype = mergeTypes(largtype, rargtype);
36953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
36963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
369761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
369861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
369961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
370061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
37013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
37023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
37033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
37043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
37052455636163fdd18581d7fdae816433f886d88213Mike Stump                           lproto->isVariadic(), lproto->getTypeQuals(),
37062455636163fdd18581d7fdae816433f886d88213Mike Stump                           NoReturn);
37073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
37083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
37103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
37113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
371272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
37133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
3714465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
37153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
37163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
37173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
37183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
37193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
37203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
37213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
37223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
37233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
37243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
37253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
37263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
37273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
37283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
37303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
37313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
37322d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getNumArgs(), proto->isVariadic(),
37332d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getTypeQuals(), NoReturn);
37343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
37353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
37373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
37382455636163fdd18581d7fdae816433f886d88213Mike Stump  return getFunctionNoProtoType(retType, NoReturn);
37393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
37403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37413d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
374243d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
374343d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
374443d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
37457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
37467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
37473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // FIXME: C++ shouldn't be going through here!  The rules are different
37483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // enough that they should be handled separately.
37497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
37507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // shouldn't be going through here!
37516217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
3752c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    LHS = RT->getPointeeType();
37536217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
3754c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    RHS = RT->getPointeeType();
37553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
37573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
37583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3759f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
37603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
37613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
37623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // If the qualifiers are different, the types aren't compatible
37645a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // Note that we handle extended qualifiers later, in the
37655a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // case for ExtQualType.
37665a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
37673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
37683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3769852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
3770852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
3771f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
37721adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
37731adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
37741adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
37751adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
37764c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
377707d258756dc856c6987c394a0972884e6ed46765Eli Friedman  // Strip off objc_gc attributes off the top level so they can be merged.
377807d258756dc856c6987c394a0972884e6ed46765Eli Friedman  // This is a complete mess, but the attribute itself doesn't make much sense.
3779585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  if (RHSClass == Type::ExtQual) {
378007d258756dc856c6987c394a0972884e6ed46765Eli Friedman    QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
378107d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (GCAttr != QualType::GCNone) {
378286f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
37831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // __weak attribute must appear on both declarations.
37841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // __strong attribue is redundant if other decl is an objective-c
378586f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // object pointer (or decorated with __strong attribute); otherwise
378686f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // issue error.
378786f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
378886f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian          (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
378914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff           !LHSCan->isObjCObjectPointerType()))
37908df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        return QualType();
37911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
379207d258756dc856c6987c394a0972884e6ed46765Eli Friedman      RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
379307d258756dc856c6987c394a0972884e6ed46765Eli Friedman                     RHS.getCVRQualifiers());
379407d258756dc856c6987c394a0972884e6ed46765Eli Friedman      QualType Result = mergeTypes(LHS, RHS);
37958df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      if (!Result.isNull()) {
37968df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        if (Result.getObjCGCAttr() == QualType::GCNone)
37978df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = getObjCGCQualType(Result, GCAttr);
37988df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        else if (Result.getObjCGCAttr() != GCAttr)
37998df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = QualType();
38008df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      }
380107d258756dc856c6987c394a0972884e6ed46765Eli Friedman      return Result;
380207d258756dc856c6987c394a0972884e6ed46765Eli Friedman    }
3803585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  }
3804585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  if (LHSClass == Type::ExtQual) {
380507d258756dc856c6987c394a0972884e6ed46765Eli Friedman    QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
380607d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (GCAttr != QualType::GCNone) {
38078df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
38088df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      // __weak attribute must appear on both declarations. __strong
38091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // __strong attribue is redundant if other decl is an objective-c
381086f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // object pointer (or decorated with __strong attribute); otherwise
381186f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      // issue error.
381286f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian      if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
381386f4385536a0b2202860ad4e20d84f9330b1a4f4Fariborz Jahanian          (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
381414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff           !RHSCan->isObjCObjectPointerType()))
38158df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        return QualType();
38161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
381707d258756dc856c6987c394a0972884e6ed46765Eli Friedman      LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
381807d258756dc856c6987c394a0972884e6ed46765Eli Friedman                     LHS.getCVRQualifiers());
381907d258756dc856c6987c394a0972884e6ed46765Eli Friedman      QualType Result = mergeTypes(LHS, RHS);
38208df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      if (!Result.isNull()) {
38218df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        if (Result.getObjCGCAttr() == QualType::GCNone)
38228df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = getObjCGCQualType(Result, GCAttr);
38238df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian        else if (Result.getObjCGCAttr() != GCAttr)
38248df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian          Result = QualType();
38258df7a28269a1c0f4444928d0baea402b410e95f1Fariborz Jahanian      }
3826354e53da656237e25518b3fafa3bf84de6e6f57eEli Friedman      return Result;
382707d258756dc856c6987c394a0972884e6ed46765Eli Friedman    }
3828585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian  }
3829585f7b2f5c818d7579cffc91590bdc9e3d8b645dFariborz Jahanian
38304c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
3831a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3832a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
3833a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3834a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
38351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3836213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
3837213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3838213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
38391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38404e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
38414e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
38421adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
38431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
38443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = LHS->getAsEnumType()) {
38453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
38463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
3847bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
38483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = RHS->getAsEnumType()) {
38493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
38503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
3851bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
38521adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
38533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
3854ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
38553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
38564a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
38571adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
385872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
385972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
386072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
386172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
386272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
386372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
386472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
386572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
38667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
38677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
386872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
386972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
387072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
387172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
387272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
387372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
387472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
387572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
387672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
387772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
387872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
38791adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
38803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
38813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
38826217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
38836217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
38843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
38853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
388607d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
388761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
388807d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
388961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
38903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
38913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
3892c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
3893c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
3894c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
38956217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
38966217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
3897c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3898c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
3899c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3900c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
3901c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3902c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
3903c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
3904c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
39051adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
39063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
39073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
39083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
39093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
39103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
39113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
39123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
39133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
39143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
39153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
391661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
391761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
391861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
391961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
39203bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
39213bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
39223bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
39233bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
39243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
39253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
392661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
392761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
392861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
392961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
39303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
39313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
39323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
39333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
39343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
39353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
39363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
39373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
39383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
39393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
39403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
39413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
39423bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
39433bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
39447e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
39457e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
39463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
39471adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
39483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return mergeFunctionTypes(LHS, RHS);
394972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
395072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
39513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
39521adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
39533cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
39543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
395564cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
395664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
395764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
39583cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
39595a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
39603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
39613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
396261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
396361490e9a965cfee8a78c12c6802138844f04250dCedric Venet  case Type::ObjCInterface: {
39645fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    // Check if the interfaces are assignment compatible.
39655a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
39665a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
39675fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
39685fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
39695fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    if (LHSIface && RHSIface &&
39705fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff        canAssignObjCInterfaces(LHSIface, RHSIface))
39715fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
39725fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
39733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
397461490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
397514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
39761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(),
397714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                RHS->getAsObjCObjectPointerType()))
397814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
397914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
3980bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
398114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
39825a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::FixedWidthInt:
39835a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // Distinct fixed-width integers are not compatible.
39845a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
39855a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::ExtQual:
39865a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: ExtQual types can be compatible even if they're not
39875a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // identical!
39885a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
39895a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // First attempt at an implementation, but I'm not really sure it's
39905a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // right...
39915a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#if 0
39925a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* LQual = cast<ExtQualType>(LHSCan);
39935a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* RQual = cast<ExtQualType>(RHSCan);
39945a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
39955a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman        LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
39965a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return QualType();
39975a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
39985a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    LHSBase = QualType(LQual->getBaseType(), 0);
39995a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    RHSBase = QualType(RQual->getBaseType(), 0);
40005a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = mergeTypes(LHSBase, RHSBase);
40015a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (ResultType.isNull()) return QualType();
40025a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
40035a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LHSCan.getUnqualifiedType() == ResCanUnqual)
40045a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return LHS;
40055a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (RHSCan.getUnqualifiedType() == ResCanUnqual)
40065a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return RHS;
40075a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
40085a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
40095a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
40105a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return ResultType;
40115a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#endif
40127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
40137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  case Type::TemplateSpecialization:
40147532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    assert(false && "Dependent types have no size");
40157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
4016ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
401772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
401872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
4019ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
40207192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
40215426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
4022ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
4023ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
402488054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
4025ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
4026ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (T == BoolTy)
4027ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
4028f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
4029f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth();
4030f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  }
4031f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
4032ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
4033ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4034ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
4035ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4036ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
4037ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (const EnumType* ETy = T->getAsEnumType())
4038ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
4039ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  const BuiltinType* BTy = T->getAsBuiltinType();
4040ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert (BTy && "Unexpected signed integer type");
4041ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
4042ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
4043ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
4044ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
4045ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
4046ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
4047ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
4048ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
4049ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
4050ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
4051ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
4052ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
40532df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
40542df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
4055ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
4056ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
4057ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
4058ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
4059ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
4060ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
40612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
40622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
40632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
406486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
406586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
406686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
406786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
406886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
406986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
407086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
407186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters.  This returns the resultant type.
40721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
407386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
407486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  bool AllowTypeModifiers = true) {
407586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
407686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
407786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
40781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the modifiers first.
408086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
408186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
408286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
40831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
408486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
408586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
408686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
408786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
408886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
408986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
409086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
409186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
409286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
409386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
409486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
409586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
409686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
409786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
409886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
409986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
410086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
410186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
41021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
410486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
410586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
410686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
410786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
410886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
410986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
411086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
411186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
411286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
411386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
411486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
411586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
411686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
411786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
411886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
411986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
412086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
412186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
412286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
412386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
412486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
412586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
412686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
412786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
412886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
412986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
413086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
413186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
413286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
413386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
413486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
413586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
413686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
413786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
413886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
413986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
414086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
414186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
414286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
414386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
414486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
414586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
414686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
414786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
414886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
414986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
415086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
415186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
415286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
415386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
415486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
415586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
415686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
415786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
415886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
415986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
416086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
416186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
416286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
416386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
416486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
416586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
416686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
416786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
416886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
416986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
417086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
417186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
417286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
417386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
417486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
417586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
417686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Type->isArrayType()) {
417786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
417886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    } else {
417986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
418086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
418186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
418286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
418386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
418486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
418586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
41861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
418786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
41881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
418986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
419086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getVectorType(ElementType, NumElements);
419186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
419286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
41939a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
4194c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
4195c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
4196f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
419786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
419886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
4199fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
42009a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
4201f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
4202782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
4203f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
4204f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
4205f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
4206fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
4207f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
4208fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
4209fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
4210fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
4211782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
42121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
421386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (!AllowTypeModifiers)
421486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return Type;
42151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
421686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Done = false;
421786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
421886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
421986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      default: Done = true; --Str; break;
422086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '*':
422186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getPointerType(Type);
422286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
422386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '&':
422486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getLValueReferenceType(Type);
422586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
422686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      // FIXME: There's no way to have a built-in with an rvalue ref arg.
422786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case 'C':
422886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Type.getQualifiedType(QualType::Const);
422986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
423086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
423186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
42321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
423386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
423486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
423586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
423686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
423786df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id,
423886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                    GetBuiltinTypeError &Error) {
423986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(id);
42401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
424186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
42421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
424386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
424486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
424586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
424686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
424786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
424886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
424986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
425086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
425186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
425286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
425386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
425486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
42551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
425686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
425786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
425886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
425986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
426086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
426186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
426286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
426386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (ArgTypes.size() == 0 && TypeStr[0] == '.')
426486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return getFunctionNoProtoType(ResType);
426586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
426686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                         TypeStr[0] == '.', 0);
426786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
4268a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4269a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType
4270a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4271a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Perform the usual unary conversions. We do this early so that
4272a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // integral promotions to "int" can allow us to exit early, in the
4273a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // lhs == rhs check. Also, for conversion purposes, we ignore any
4274a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // qualifiers.  For example, "const float" and "float" are
4275a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // equivalent.
4276a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isPromotableIntegerType())
4277a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = getPromotedIntegerType(lhs);
4278a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4279a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = lhs.getUnqualifiedType();
4280a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (rhs->isPromotableIntegerType())
4281a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = getPromotedIntegerType(rhs);
4282a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4283a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = rhs.getUnqualifiedType();
4284a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4285a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If both types are identical, no conversion is needed.
4286a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs == rhs)
4287a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
42881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4289a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4290a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The caller can deal with this (e.g. pointer + int).
4291a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4292a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
42931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // At this point, we have two different arithmetic types.
42951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4296a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Handle complex types first (C99 6.3.1.8p1).
4297a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexType() || rhs->isComplexType()) {
4298a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the complex type.
42991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
4300a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
4301a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4302a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
43031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
4304a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
4305a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4306a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4307a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // This handles complex/complex, complex/float, or float/complex.
43081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // When both operands are complex, the shorter operand is converted to the
43091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // type of the longer, and that is the type of the result. This corresponds
43101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // to what is done when combining two real floating-point operands.
43111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // The fun begins when size promotion occur across type domains.
4312a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // From H&S 6.3.4: When one operand is complex and the other is a real
43131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // floating-point type, the less precise type is converted, within it's
4314a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // real or complex domain, to the precision of the other type. For example,
43151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // when combining a "long double" with a "double _Complex", the
4316a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // "double _Complex" is promoted to "long double _Complex".
4317a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
43181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (result > 0) { // The left side is bigger, convert rhs.
4320a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
43211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (result < 0) { // The right side is bigger, convert lhs.
4322a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
43231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
4324a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // At this point, lhs and rhs have the same rank/size. Now, make sure the
4325a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // domains match. This is a requirement for our implementation, C99
4326a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // does not require this promotion.
4327a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4328a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4329a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return rhs;
4330a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      } else { // handle "_Complex double, double".
4331a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs;
4332a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      }
4333a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4334a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs; // The domain/size match exactly.
4335a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4336a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Now handle "real" floating types (i.e. float, double, long double).
4337a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4338a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the real floating type.
4339a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isIntegerType()) {
4340a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the lhs floating point type.
4341a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4342a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4343a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isComplexIntegerType()) {
4344a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the complex floating point type.
4345a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(lhs);
4346a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4347a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isIntegerType()) {
4348a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the rhs floating point type.
4349a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4350a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
43511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isComplexIntegerType()) {
4352a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the complex floating point type.
4353a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(rhs);
4354a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4355a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // We have two real floating types, float/complex combos were handled above.
4356a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Convert the smaller operand to the bigger result.
4357a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
4358a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (result > 0) // convert the rhs
4359a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4360a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    assert(result < 0 && "illegal float comparison");
4361a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return rhs;   // convert the lhs
4362a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4363a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4364a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Handle GCC complex int extension.
4365a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4366a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4367a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4368a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhsComplexInt && rhsComplexInt) {
43691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
4370a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman                              rhsComplexInt->getElementType()) >= 0)
4371a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs; // convert the rhs
4372a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4373a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (lhsComplexInt && rhs->isIntegerType()) {
4374a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
4375a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4376a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (rhsComplexInt && lhs->isIntegerType()) {
4377a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
4378a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4379a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4380a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4381a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Finally, we have two differing integer types.
4382a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The rules for this case are in C99 6.3.1.8
4383a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  int compare = getIntegerTypeOrder(lhs, rhs);
4384a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  bool lhsSigned = lhs->isSignedIntegerType(),
4385a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman       rhsSigned = rhs->isSignedIntegerType();
4386a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  QualType destType;
4387a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhsSigned == rhsSigned) {
4388a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Same signedness; use the higher-ranked type
4389a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = compare >= 0 ? lhs : rhs;
4390a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (compare != (lhsSigned ? 1 : -1)) {
4391a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The unsigned type has greater than or equal rank to the
4392a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // signed type, so use the unsigned type
4393a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? rhs : lhs;
4394a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4395a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The two types are different widths; if we are here, that
4396a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // means the signed type is larger than the unsigned type, so
4397a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // use the signed type.
4398a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? lhs : rhs;
4399a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else {
4400a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The signed type is higher-ranked than the unsigned type,
4401a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // but isn't actually any bigger (like unsigned int and long
4402a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // on most 32-bit systems).  Use the unsigned type corresponding
4403a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // to the signed type.
4404a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4405a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4406a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return destType;
4407a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
4408