ASTContext.cpp revision dbe833da54e1b6192991b64fc453cd50b5ee7909
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
17aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
18e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/RecordLayout.h"
21a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
2385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
246fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
25557c5b1717bc8919b1b40cf2064b51491ec53a44Chris Lattner#include "llvm/Support/MemoryBuffer.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3261710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
3361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner                       TargetInfo &t,
34e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
352deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor                       bool FreeMem, unsigned size_reserve,
362deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor                       bool InitializeBuiltins) :
37ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource(0) {
41e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  if (size_reserve > 0) Types.reserve(size_reserve);
42e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  InitBuiltinTypes();
43e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
447a9cbedca55edd4a11330c2426298a3033a873b5Douglas Gregor  BuiltinInfo.InitializeTargetBuiltins(Target);
452deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  if (InitializeBuiltins)
462deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor    this->InitializeBuiltins(idents);
47e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
48e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Deallocate all the types.
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!Types.empty()) {
524b05b1dee6cc65ae61d93dab7edff72710f24589Ted Kremenek    Types.back()->Destroy(*this);
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Types.pop_back();
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
55b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
56b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
57b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
60b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
62b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
63b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
64b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
65b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
66b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar    llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar      I = ObjCLayouts.begin(), E = ObjCLayouts.end();
68b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
69b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
71b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
72b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
73b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
74ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
751ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
761ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
771ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNSEnd = NestedNameSpecifiers.end();
78e7dcd782df0e1cc3a63e429a866098e69db39153Douglas Gregor       NNS != NNSEnd;
791ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor       /* Increment in loop */)
801ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
81ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
82ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
83ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
84ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
85b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
882deaea37a637dd01221d0cced343702a39d8132cDouglas Gregorvoid ASTContext::InitializeBuiltins(IdentifierTable &idents) {
892deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor  BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
902deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor}
912deaea37a637dd01221d0cced343702a39d8132cDouglas Gregor
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid
932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
1007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
101dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
102dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent) 0,
103dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
104dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
105dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
106dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
107c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
110dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
113dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
114dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
115dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
116dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
117dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
118dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
119dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
120dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
121dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
122dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor
123dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
133f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
14598be4943e8dc4f3905629a7102668960873cf863Chris Lattner  if (Target.isCharSigned())
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
16764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
1682df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
1692df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
1702df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
1712df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
1723a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
1733a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
1743a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
1753a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
17664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
1778e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
178898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
179898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
180898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
181898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
182898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
183898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
184898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
185898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
1868e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
1918e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
1927e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
193a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCIdType = QualType();
1947e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  IdStructType = 0;
195a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCClassType = QualType();
1968baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  ClassStructType = 0;
1978baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
198a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
19933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
20033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
20133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2026e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2036e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2046e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
207464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
208464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
209464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
210a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
211b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
212b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
213b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
214b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  const BuiltinType *BT = T->getAsBuiltinType();
215b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
216b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
217b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
218b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
219b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
220b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
221b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
222b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
223b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
224af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// getDeclAlign - Return a conservative estimate of the alignment of the
225af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
226af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
227b7d0844c887a72064b624dc6df12cbe1441f69d0Daniel Dunbarunsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
228dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
229dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
230dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
231dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    Align = std::max(Align, AA->getAlignment());
232dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
233af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
234af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
2354cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson    if (const ReferenceType* RT = T->getAsReferenceType()) {
2364cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      unsigned AS = RT->getPointeeType().getAddressSpace();
237f0930235ce58a91aa3b840bece9052f44d630536Anders Carlsson      Align = Target.getPointerAlign(AS);
2384cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson    } else if (!T->isIncompleteType() && !T->isFunctionType()) {
2394cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
240dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
241dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
242dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
243dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
244dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
245af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
246dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
247dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  return Align / Target.getCharWidth();
248af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
249b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
250a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
251a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
252d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
2531d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
2545e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
2555e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
256a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
25772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
25872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
25918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
26072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
26172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
26218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
26372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
26472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
2655d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
2665d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
26718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
26818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
26918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
27018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
27118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
27272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
273fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
27418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
27518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
27618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
27718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
278fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
2791d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
280030d8846c7e520330007087e949f621989876e3aChris Lattner
28198be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
2829e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
283030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
284030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
2855c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
286213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
287030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
288030d8846c7e520330007087e949f621989876e3aChris Lattner    std::pair<uint64_t, unsigned> EltInfo =
28998be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<VectorType>(T)->getElementType());
2909e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
2914bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
2926fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
2936fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
2946fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // FIXME: this should probably be a target property.
2956fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    Align = 1 << llvm::Log2_32_Ceil(Align);
296030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
297030d8846c7e520330007087e949f621989876e3aChris Lattner  }
2985d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
2999e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
300a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
301692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
302d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
30318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
30418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
30518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
30618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
30718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
3086f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
3099e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
3109e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
3116f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
312692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
313692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
314692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
3156f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
3169e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
3179e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
3186f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
31964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
32064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
32164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
32264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
323692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
3246f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
3259e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
3269e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
3276f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
328692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
3296f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
3309e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
3319e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
3326f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
333692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
3346f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
3359e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
3369e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
3376f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
338692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
3396f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
3409e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
3419e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
3426f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
343ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
344ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
345ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
346ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
347ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
3486f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
3499e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
3509e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
3516f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
3526f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
3535426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
3545426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
3556f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
3566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
3579e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
3589e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
3596f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
3606e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
3616e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
3626e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
363a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
364bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
365f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  case Type::FixedWidthInt:
366f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // FIXME: This isn't precisely correct; the width/alignment should depend
367f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // on the available types for the target
368f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Width = cast<FixedWidthIntType>(T)->getWidth();
369736166b38235cf6d0ffb67638960d95fb2afcbd6Chris Lattner    Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
370f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Align = Width;
371f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    break;
372f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  case Type::ExtQual:
37398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // FIXME: Pointers into different addr spaces could have different sizes and
37498be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // alignment requirements: getPointerInfo should take an AddrSpace.
375f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
376a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  case Type::ObjCQualifiedId:
37772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ObjCQualifiedInterface:
3785426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
379f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
3806f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
381485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
382485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
383485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
384485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
385485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
386485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
387f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
388f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
3895426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
390f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
391f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
392f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
3937c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
3947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
3957ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner    // "When applied to a reference or a reference type, the result is the size
3965d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // of the referenced type." C++98 5.3.3p2: expr.sizeof.
3976f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // FIXME: This is wrong for struct layout: a reference in a struct has
3986f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // pointer size.
399bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
400f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
4011cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
4021cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
4031cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // If we ever want to support other ABIs this needs to be abstracted.
4041cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson
405f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
4061cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    std::pair<uint64_t, unsigned> PtrDiffInfo =
4071cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
4081cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
409f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
410f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
4111cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
4121cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
413f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
4145d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
4155d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
4165d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
4175d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    std::pair<uint64_t, unsigned> EltInfo =
41898be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
4199e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
4205d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
4215d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
4225d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
42344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
4241d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
42544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
42644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
42744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
42844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
42944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
43072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
43172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
4321d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
4331d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
4341d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
4358389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
4368389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
4378389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
4388389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
4398389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner
4401d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
4417176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
4427176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
4431d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
4447176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
4457176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
4467176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
447dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
448a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
4497532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
45018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
45118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
45218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
45318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = Aligned->getAlignment();
45418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
45518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
45618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
4577532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
4587176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
45918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
46018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
46118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
46218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
46318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
46418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
46518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
46618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
46718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::QualifiedName:
46818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
46918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
47018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
47118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(getCanonicalType(T) != T &&
47218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
47318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
47418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
47518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
47618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
47718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
478d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
479464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
4809e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
481a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
482a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
48334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
48434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
48534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
48634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
48734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
48834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
4891eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
4901eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
4911eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (const ComplexType* CT = T->getAsComplexType())
4921eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
4931eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
4941eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
4951eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
4961eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
49734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
49834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
49934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
50034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
5018b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel/// LayoutField - Field layout.
5028b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patelvoid ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
5033b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar                                  bool IsUnion, unsigned StructPacking,
5048b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel                                  ASTContext &Context) {
5053b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  unsigned FieldPacking = StructPacking;
5068b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  uint64_t FieldOffset = IsUnion ? 0 : Size;
5078b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  uint64_t FieldSize;
5088b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  unsigned FieldAlign;
5093b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar
5103b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  // FIXME: Should this override struct packing? Probably we want to
5113b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  // take the minimum?
5123b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
5133b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    FieldPacking = PA->getAlignment();
5148b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5158b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  if (const Expr *BitWidthExpr = FD->getBitWidth()) {
5168b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    // TODO: Need to check this algorithm on other targets!
5178b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    //       (tested on Linux-X86)
5189a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman    FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
5198b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5208b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    std::pair<uint64_t, unsigned> FieldInfo =
5218b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      Context.getTypeInfo(FD->getType());
5228b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    uint64_t TypeSize = FieldInfo.first;
5238b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5243b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // Determine the alignment of this bitfield. The packing
5253b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // attributes define a maximum and the alignment attribute defines
5263b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // a minimum.
5273b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // FIXME: What is the right behavior when the specified alignment
5283b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // is smaller than the specified packing?
5298b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    FieldAlign = FieldInfo.second;
5303b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    if (FieldPacking)
5313b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar      FieldAlign = std::min(FieldAlign, FieldPacking);
5328b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
5338b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldAlign = std::max(FieldAlign, AA->getAlignment());
5348b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5358b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    // Check if we need to add padding to give the field the correct
5368b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    // alignment.
5378b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
5388b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
5398b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5408b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    // Padding members don't affect overall alignment
5418b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    if (!FD->getIdentifier())
5428b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldAlign = 1;
5438b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  } else {
5448389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    if (FD->getType()->isIncompleteArrayType()) {
5458389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      // This is a flexible array member; we can't directly
5468b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      // query getTypeInfo about these, so we figure it out here.
5478b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      // Flexible array members don't have any size, but they
5488b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      // have to be aligned appropriately for their element type.
5498b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldSize = 0;
550c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      const ArrayType* ATy = Context.getAsArrayType(FD->getType());
5518b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldAlign = Context.getTypeAlign(ATy->getElementType());
5522f1169f45b915c40c6f330af69e4e13dd18a5084Anders Carlsson    } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
5532f1169f45b915c40c6f330af69e4e13dd18a5084Anders Carlsson      unsigned AS = RT->getPointeeType().getAddressSpace();
5542f1169f45b915c40c6f330af69e4e13dd18a5084Anders Carlsson      FieldSize = Context.Target.getPointerWidth(AS);
5552f1169f45b915c40c6f330af69e4e13dd18a5084Anders Carlsson      FieldAlign = Context.Target.getPointerAlign(AS);
5568b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    } else {
5578b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      std::pair<uint64_t, unsigned> FieldInfo =
5588b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel        Context.getTypeInfo(FD->getType());
5598b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldSize = FieldInfo.first;
5608b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldAlign = FieldInfo.second;
5618b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    }
5628b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5633b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // Determine the alignment of this bitfield. The packing
5643b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // attributes define a maximum and the alignment attribute defines
5653b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // a minimum. Additionally, the packing alignment must be at least
5663b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // a byte for non-bitfields.
5673b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    //
5683b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // FIXME: What is the right behavior when the specified alignment
5693b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    // is smaller than the specified packing?
5703b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    if (FieldPacking)
5713b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar      FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
5728b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
5738b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel      FieldAlign = std::max(FieldAlign, AA->getAlignment());
5748b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5758b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    // Round up the current record size to the field's alignment boundary.
5768b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
5778b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  }
5788b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5798b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  // Place this field at the current location.
5808b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  FieldOffsets[FieldNo] = FieldOffset;
5818b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
5828b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  // Reserve space for this field.
5838b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  if (IsUnion) {
5848b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    Size = std::max(Size, FieldSize);
5858b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  } else {
5868b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    Size = FieldOffset + FieldSize;
5878b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  }
5888b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
589d6884a0b90091a063478ec7fa31f6d76c94e4917Daniel Dunbar  // Remember the next available offset.
590d6884a0b90091a063478ec7fa31f6d76c94e4917Daniel Dunbar  NextOffset = Size;
591d6884a0b90091a063478ec7fa31f6d76c94e4917Daniel Dunbar
5928b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  // Remember max struct/class alignment.
5938b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  Alignment = std::max(Alignment, FieldAlign);
5948b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel}
5958b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel
596a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
597a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
598a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
599a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
600a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
601f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
602a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
603a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
604a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
605a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
606a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
607a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
608a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
609a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
610a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
611a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
612a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
613a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
6149820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
6159820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
6169820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
6179820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       E = PD->prop_end(*this); I != E; ++I)
6189820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
6199820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
6209820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
6219820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into nested protocols.
6229820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
6239820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
6249820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(*P, Ivars);
6259820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
6269820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
6279820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// CollectSynthesizedIvars -
6289820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// This routine collect synthesized ivars for the designated class.
6299820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
6309820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
6319820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
6329820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
6339820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       E = OI->prop_end(*this); I != E; ++I) {
6349820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
6359820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
6369820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
6379820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into interface's protocol list for properties declared
6389820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // in the protocol and whose ivars are synthesized.
6399820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
6409820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
6419820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    ObjCProtocolDecl *PD = (*P);
6429820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(PD, Ivars);
6439820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
6449820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
6459820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
646b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the
647b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface.
648b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar///
649b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's
650b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars.
65144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout &
652b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
653b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar                          const ObjCImplementationDecl *Impl) {
654532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar  assert(!D->isForwardDecl() && "Invalid interface decl!");
655532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar
65644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Look up this layout, if already laid out, return what we have.
657d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  ObjCContainerDecl *Key =
658d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
659d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
660d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    return *Entry;
66144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
662453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  unsigned FieldCount = D->ivar_size();
663453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // Add in synthesized ivar count if laying out an implementation.
664453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
6659820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
6669820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectSynthesizedIvars(D, Ivars);
6679820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    FieldCount += Ivars.size();
668d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    // If there aren't any sythesized ivars then reuse the interface
669453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entry. Note we can't cache this because we simply free all
670453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entries later; however we shouldn't look up implementations
671453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // frequently.
672453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    if (FieldCount == D->ivar_size())
673453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar      return getObjCLayout(D, 0);
674453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  }
675453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar
6766a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel  ASTRecordLayout *NewEntry = NULL;
6776a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel  if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
6786a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel    const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
6796a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel    unsigned Alignment = SL.getAlignment();
680d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar
681913af35741b55fe854bc26c9dddc312cd991e24dDaniel Dunbar    // We start laying out ivars not at the end of the superclass
682913af35741b55fe854bc26c9dddc312cd991e24dDaniel Dunbar    // structure, but at the next byte following the last field.
683913af35741b55fe854bc26c9dddc312cd991e24dDaniel Dunbar    uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
684d6884a0b90091a063478ec7fa31f6d76c94e4917Daniel Dunbar
685d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
6866a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel    NewEntry->InitializeLayout(FieldCount);
6876a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel  } else {
688d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
6896a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel    NewEntry->InitializeLayout(FieldCount);
6906a5a34c0e5587263df719c62030761cf59d27fe4Devang Patel  }
69144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
6923b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  unsigned StructPacking = 0;
6933b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  if (const PackedAttr *PA = D->getAttr<PackedAttr>())
6943b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    StructPacking = PA->getAlignment();
69544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
69644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
69744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
69844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel                                    AA->getAlignment()));
69944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
70044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Layout each ivar sequentially.
70144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  unsigned i = 0;
70244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
70344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel       IVE = D->ivar_end(); IVI != IVE; ++IVI) {
70444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ObjCIvarDecl* Ivar = (*IVI);
7053b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
70644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
707453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // And synthesized ivars, if this is an implementation.
708453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
7099820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    // FIXME. Do we need to colltect twice?
7109820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
7119820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectSynthesizedIvars(D, Ivars);
7129820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
7139820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
71418191886554407b4260c40165bc294c3ea732033Fariborz Jahanian  }
71599eee3675e830ff367ae1fe35ce19fb49e7e4114Fariborz Jahanian
71644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Finally, round the size of the total struct up to the alignment of the
71744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // struct itself.
71844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  NewEntry->FinalizeLayout();
71944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  return *NewEntry;
72044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel}
72144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
722b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
723b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
724b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
725b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
726b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
727b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
728b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
729b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
730b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
731b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
73288a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
733464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
734464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
73598be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
7364b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  D = D->getDefinition(*this);
7374b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  assert(D && "Cannot get layout of forward declarations!");
7384bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
739464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
74088a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
741464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
7424bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
74388a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  // Allocate and assign into ASTRecordLayouts here.  The "Entry" reference can
74488a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
74588a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  ASTRecordLayout *NewEntry = new ASTRecordLayout();
746464175bba1318bef7905122e9fda20cff926df78Chris Lattner  Entry = NewEntry;
7474bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
748e267ff35b2f4e9d2b0d8bf24109d41cc7398b61bDouglas Gregor  // FIXME: Avoid linear walk through the fields, if possible.
7496ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
7506ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                           D->field_end(*this)));
75139ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis  bool IsUnion = D->isUnion();
752464175bba1318bef7905122e9fda20cff926df78Chris Lattner
7533b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  unsigned StructPacking = 0;
7543b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar  if (const PackedAttr *PA = D->getAttr<PackedAttr>())
7553b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar    StructPacking = PA->getAlignment();
7563b0db908ebd07eaa26bc90deba5e826de00fe515Daniel Dunbar
7574bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman  if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
7588b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel    NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
7598b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel                                    AA->getAlignment()));
7604bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
7614bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman  // Layout each field, for now, just sequentially, respecting alignment.  In
7624bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman  // the future, this will need to be tweakable by targets.
76344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor  unsigned FieldIdx = 0;
7646ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor  for (RecordDecl::field_iterator Field = D->field_begin(*this),
7656ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                               FieldEnd = D->field_end(*this);
76644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor       Field != FieldEnd; (void)++Field, ++FieldIdx)
76744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
7684bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
7694bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman  // Finally, round the size of the total struct up to the alignment of the
7704bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman  // struct itself.
7718b27704d61be128bbff6352e6ef417efc0d6bf4fDevang Patel  NewEntry->FinalizeLayout();
7725d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
773464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
774464175bba1318bef7905122e9fda20cff926df78Chris Lattner
775a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
776a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
777a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
778a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
779f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
780f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
781f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
782f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
783b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
784b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
785b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
786b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
787b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttr = QualType::GCNone;
788b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
789b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
790b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
791b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // If this type already has an address space specified, it cannot get
792b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
793b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getAddressSpace() == 0 &&
794b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner           "Type cannot be in multiple addr spaces!");
795b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttr = EQT->getObjCGCAttr();
796b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
797b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
798f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner
799b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // Check if we've already instantiated this type.
800ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  llvm::FoldingSetNodeID ID;
801b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
802ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  void *InsertPos = 0;
803f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
804b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
805b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
806ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // If the base type isn't canonical, this won't be a canonical type either,
807ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // so fill in the canonical type field.
808ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  QualType Canonical;
809b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (!TypeNode->isCanonical()) {
810f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    Canonical = getAddrSpaceQualType(CanT, AddressSpace);
811ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
812b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
813f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
814f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
815ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  }
816b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
817b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
818f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
819ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  Types.push_back(New);
820b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
821ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
822ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
823b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
824b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner                                       QualType::GCAttrTypes GCAttr) {
825d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
826b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
827d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
828d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
829b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // If we are composing extended qualifiers together, merge together into one
830b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  // ExtQualType node.
831b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
832b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  Type *TypeNode = T.getTypePtr();
833b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  unsigned AddressSpace = 0;
834b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
835b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
836b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // If this type already has an address space specified, it cannot get
837b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // another one.
838b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    assert(EQT->getObjCGCAttr() == QualType::GCNone &&
839b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner           "Type cannot be in multiple addr spaces!");
840b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    AddressSpace = EQT->getAddressSpace();
841b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    TypeNode = EQT->getBaseType();
842b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  }
843d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
844d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // Check if we've already instantiated an gc qual'd type of this type.
845d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  llvm::FoldingSetNodeID ID;
846b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
847d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  void *InsertPos = 0;
848d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
849b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    return QualType(EXTQy, CVRQuals);
850d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
851d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // If the base type isn't canonical, this won't be a canonical type either,
852d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  // so fill in the canonical type field.
8535a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // FIXME: Isn't this also not canonical if the base type is a array
8545a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // or pointer type?  I can't find any documentation for objc_gc, though...
855d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType Canonical;
856d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  if (!T->isCanonical()) {
857b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    Canonical = getObjCGCQualType(CanT, GCAttr);
858d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian
859b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    // Update InsertPos, the previous call could have invalidated it.
860d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
861d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
862d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  }
863b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  ExtQualType *New =
864b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
865d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  ExtQualTypes.InsertNode(New, InsertPos);
866d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  Types.push_back(New);
867b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return QualType(New, CVRQuals);
868d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
869a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
8735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
886f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
890f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
892f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ComplexType *New = new (*this,8) ComplexType(T, Canonical);
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
898f98aba35e6c3da5aae61843fc01334939e4e12ecEli FriedmanQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
899f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
900f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman     SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
901f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  FixedWidthIntType *&Entry = Map[Width];
902f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (!Entry)
903f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Entry = new FixedWidthIntType(Width, Signed);
904f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  return QualType(Entry, 0);
905f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman}
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
9175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
9185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
9205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
9225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
923f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
927f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
929f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  PointerType *New = new (*this,8) PointerType(T, Canonical);
9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9355618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// getBlockPointerType - Return the uniqued reference to the type for
9365618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
9375618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
938296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
939296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
9405618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
9415618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
9425618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
9435618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
9445618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
9455618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
9465618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
9475618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
9485618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
949296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // If the block pointee type isn't canonical, this won't be a canonical
9505618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
9515618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
9525618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (!T->isCanonical()) {
9535618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
9545618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
9555618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
9565618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
9575618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
958f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
9595618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
960f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
9615618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
9625618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
9635618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
9645618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
9655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
9667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
9677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
9687c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getLValueReferenceType(QualType T) {
9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceType::Profile(ID, T);
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
9757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
9767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
9787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
9795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
9837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getLValueReferenceType(getCanonicalType(T));
9847c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
9857c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
9867c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
9877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
9887c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
9897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
9907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
9917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
9927c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
9937c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
9947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
9957c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
9967c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
9977c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
9987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
9997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
10007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
10017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
10027c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
10037c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  ReferenceType::Profile(ID, T);
10047c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
10057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
10067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
10077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
10087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
10097c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
10107c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
10117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
10127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
10137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (!T->isCanonical()) {
10147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getRValueReferenceType(getCanonicalType(T));
10157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
10165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
10177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
10187c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1019f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
10205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
10235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
10247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
10255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
10265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1028f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1029f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
1030f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian RedlQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1031f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl{
1032f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1033f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1034f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1035f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1036f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1037f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1038f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1039f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1040f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1041f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1042f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1043f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1044f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
1045f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (!T->isCanonical()) {
1046f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1047f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1048f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1049f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1050f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1051f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1052f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
1053f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
1054f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1055f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1056f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1057f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1058f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1059fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// getConstantArrayType - Return the unique reference to the type for an
1060fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
1061fb22d96692c5240fb8d611290dbf7eeed3759c73Steve NaroffQualType ASTContext::getConstantArrayType(QualType EltTy,
106238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1063c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1064c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
106538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
106638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
106738aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
106838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
106938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
10710be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
10747192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  if (ConstantArrayType *ATP =
10757192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
10765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
10775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
10795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
10805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!EltTy->isCanonical()) {
1082f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1083c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
10857192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek    ConstantArrayType *NewIP =
10867192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1087f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1090566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  ConstantArrayType *New =
1091f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
10927192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
10935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
10945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
10955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1097bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1098bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
1099c9406125e2cac9208098655ac8058c095c2c3a65Steve NaroffQualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1100c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1101c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1102c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1103c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1104c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1105566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  VariableArrayType *New =
1106f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
1107c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1108c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1109c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1110c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1111c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1112c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1113898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1114898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
1115898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// type. FIXME: We will need these to be uniqued, or at least
1116898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// comparable, at some point.
1117898574e7496ba8fd76290079d3a9d06954992734Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1118898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
1119898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                unsigned EltTypeQuals) {
1120898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1121898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1122898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1123898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Since we don't unique expressions, it isn't possible to unique
1124898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // dependently-sized array types.
1125898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1126566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  DependentSizedArrayType *New =
1127f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1128f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff                                            ASM, EltTypeQuals);
1129898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1130898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  DependentSizedArrayTypes.push_back(New);
1131898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1132898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1133898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1134898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1135c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1136c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1137c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1138c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
11390be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1140c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1141c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
1142c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (IncompleteArrayType *ATP =
1143c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1144c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1145c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1146c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1147c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1148c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1149c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1150c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (!EltTy->isCanonical()) {
1151f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
11522bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1153c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1154c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1155c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1156c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1157f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11582bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1159c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1160f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
1161566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek                                                           ASM, EltTypeQuals);
1162c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1163c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1164c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1165c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1166fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1167fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
116873322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
116973322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
117073322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1173f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
117473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
117873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorType::Profile(ID, vecType, NumElts, Type::Vector);
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
11845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!vecType->isCanonical()) {
1187f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getVectorType(getCanonicalType(vecType), NumElts);
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1191f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1193f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1199213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
120073322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1201213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
120273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
120373322924127c873c13101b705dd823f5539ffa5fSteve Naroff
1204f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1205213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
120673322924127c873c13101b705dd823f5539ffa5fSteve Naroff
120773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
120873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
1209213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
121073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
121173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
121273322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
121373322924127c873c13101b705dd823f5539ffa5fSteve Naroff
121473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
121573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
121673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
121773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (!vecType->isCanonical()) {
1218213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
121973322924127c873c13101b705dd823f5539ffa5fSteve Naroff
122073322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
122173322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1222f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
122373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
1224f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
122573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
122673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
122773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
122873322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
122973322924127c873c13101b705dd823f5539ffa5fSteve Naroff
123072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
123272564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
12335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
12345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
123672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoType::Profile(ID, ResultTy);
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
123972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (FunctionNoProtoType *FT =
124072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
12415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!ResultTy->isCanonical()) {
124572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
124872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
124972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1250f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
125372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
12545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
125572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
12565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
12605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
126161710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1262971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1263bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl                                     unsigned TypeQuals) {
12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
126772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1268bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl                             TypeQuals);
12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
127172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  if (FunctionProtoType *FTP =
127272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
12735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Determine whether the type being created is already canonical or not.
12765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCanonical = ResultTy->isCanonical();
12775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
12785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!ArgArray[i]->isCanonical())
12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isCanonical) {
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
1287f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner      CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
1288bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl
1289f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1290beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
1291bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl                                isVariadic, TypeQuals);
1292bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl
12935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
129472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
129572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1296f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1298bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl
129972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1300bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl  // for a variable size array (for parameter types) at the end of them.
130172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType *FTP =
1302bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1303bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl                                 NumArgs*sizeof(QualType), 8);
130472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1305bfa2fcba545fb270f31205d616846c2bfaf4e47eSebastian Redl                              TypeQuals, Canonical);
13065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
130772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13112ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
13122ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
13134b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
13141e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
13152ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
13162ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
13171e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
13182ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1319fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  else if (isa<TemplateTypeParmDecl>(Decl)) {
1320fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    assert(false && "Template type parameter types are always available.");
1321fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
13222ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getObjCInterfaceType(ObjCInterface);
132349aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1324c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1325566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1326566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1327f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1328f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) RecordType(Record);
1329566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  }
1330566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1331566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1332566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1333f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1334f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) EnumType(Enum);
13354b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  }
133649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  else
13372ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    assert(false && "TypeDecl without a type?");
133849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
13394b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
134049aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
13412ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
13422ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) {
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1348f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
134972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1354a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCInterfaceType - Return the unique reference to the type for the
13553536b443bc50d58a79f14fca9b6842541a434854Steve Naroff/// specified ObjC interface decl.
13563b3a45858c6b2a45114e91902c3bf3c4b7f5f302Daniel DunbarQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
13573536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
13583536b443bc50d58a79f14fca9b6842541a434854Steve Naroff
13593b3a45858c6b2a45114e91902c3bf3c4b7f5f302Daniel Dunbar  ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
13603b3a45858c6b2a45114e91902c3bf3c4b7f5f302Daniel Dunbar  Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
13613536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  Types.push_back(Decl->TypeForDecl);
13623536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  return QualType(Decl->TypeForDecl, 0);
13633536b443bc50d58a79f14fca9b6842541a434854Steve Naroff}
13643536b443bc50d58a79f14fca9b6842541a434854Steve Naroff
1365fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
1366fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// parameter with the given depth, index, and (optionally) name.
1367fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas GregorQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1368fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1369fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
1370fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1371fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
1372fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmType *TypeParm
1373fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1374fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1375fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1376fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
1377fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1378fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (Name)
1379fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1380fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                         getTemplateTypeParmType(Depth, Index));
1381fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  else
1382fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1383fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1384fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1385fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1386fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1387fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1388fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1389fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
139055f6b14230c94272efbbcdd89a92224c8db9f225Douglas GregorQualType
13917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
13927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
13937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
13947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          QualType Canon) {
139540808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor  if (!Canon.isNull())
139640808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor    Canon = getCanonicalType(Canon);
1397fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
139855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  llvm::FoldingSetNodeID ID;
13997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
140040808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor
140155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  void *InsertPos = 0;
14027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  TemplateSpecializationType *Spec
14037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
140455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
140555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  if (Spec)
140655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor    return QualType(Spec, 0);
140755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
14087532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
140940808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
141040808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                       8);
14117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
141255f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
14137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
141455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
141555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  return QualType(Spec, 0);
141655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
141755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
1418e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas GregorQualType
1419ab452ba8323d1985e08bade2bced588cddf2cc28Douglas GregorASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
1420e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor                                 QualType NamedType) {
1421e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1422ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  QualifiedNameType::Profile(ID, NNS, NamedType);
1423e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1424e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
1425e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameType *T
1426e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1427e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1428e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1429e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1430ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  T = new (*this) QualifiedNameType(NNS, NamedType,
1431ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor                                    getCanonicalType(NamedType));
1432e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1433e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameTypes.InsertNode(T, InsertPos);
1434e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1435e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1436e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1437d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1438d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     const IdentifierInfo *Name,
1439d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     QualType Canon) {
1440d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1441d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1442d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1443d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1444d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (CanonNNS != NNS)
1445d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Canon = getTypenameType(CanonNNS, Name);
1446d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1447d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1448d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
1449d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType::Profile(ID, NNS, Name);
1450d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1451d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
1452d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType *T
1453d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1454d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1455d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1456d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1457d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  T = new (*this) TypenameType(NNS, Name, Canon);
1458d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
1459d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
1460d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return QualType(T, 0);
1461d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1462d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
14631734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorQualType
14641734317845d60307d474b5da8a8d33adbaf5e723Douglas GregorASTContext::getTypenameType(NestedNameSpecifier *NNS,
14651734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            const TemplateSpecializationType *TemplateId,
14661734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            QualType Canon) {
14671734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
14681734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
14691734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
14701734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
14711734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
14721734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
14731734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
14741734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor        = CanonType->getAsTemplateSpecializationType();
14751734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
14761734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
14771734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Canon = getTypenameType(CanonNNS, CanonTemplateId);
14781734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
14791734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
14801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
14811734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  llvm::FoldingSetNodeID ID;
14821734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType::Profile(ID, NNS, TemplateId);
14831734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
14841734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  void *InsertPos = 0;
14851734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType *T
14861734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
14871734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (T)
14881734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    return QualType(T, 0);
14891734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
14901734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  T = new (*this) TypenameType(NNS, TemplateId, Canon);
14911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
14921734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
14931734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  return QualType(T, 0);
14941734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
14951734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
149688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
149788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
149888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
149988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
15002e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
150188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
150288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
150388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
150488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
150588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
150688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
150788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
150888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
150988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
151088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
151188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
151288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
151388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
151488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
151588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1516065f0d7b00c3cd2b3139ebd105f50462fc778859Chris Lattner/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1517065f0d7b00c3cd2b3139ebd105f50462fc778859Chris Lattner/// the given interface decl and the conforming protocol list.
1518a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1519a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                       ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
152088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort the protocol list alphabetically to canonicalize it.
152188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  SortAndUniqueProtocols(Protocols, NumProtocols);
152288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
15234b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
1524b0489814dcb3cb801eeb0784cc6b6fcf575ff71dChris Lattner  ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
15254b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
15264b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
1527a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  if (ObjCQualifiedInterfaceType *QT =
1528a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
15294b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
15304b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
15314b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  // No Match;
1532a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedInterfaceType *QType =
1533f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
1534566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek
15354b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  Types.push_back(QType);
1536a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
15374b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  return QualType(QType, 0);
15384b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian}
15394b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
154088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
154188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// and the conforming protocol list.
154262f5f7ffad57e0c2af2b308af3735351505937cbChris LattnerQualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
1543c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian                                            unsigned NumProtocols) {
154488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort the protocol list alphabetically to canonicalize it.
154588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  SortAndUniqueProtocols(Protocols, NumProtocols);
154688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1547c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  llvm::FoldingSetNodeID ID;
1548a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
1549c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian
1550c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  void *InsertPos = 0;
1551a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  if (ObjCQualifiedIdType *QT =
155262f5f7ffad57e0c2af2b308af3735351505937cbChris Lattner        ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
1553c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    return QualType(QT, 0);
1554c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian
1555c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  // No Match;
1556566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  ObjCQualifiedIdType *QType =
1557f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
1558c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  Types.push_back(QType);
1559a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
1560c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  return QualType(QType, 0);
1561c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian}
1562c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian
156372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
156472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
15659752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
15669752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// DeclRefExpr's. This doesn't effect the type checker, since it operates
15679752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
156872564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
1569f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofExpr->getType());
157072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
15719752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
15729752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
1573d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
1574d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
15759752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
15769752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
15779752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
15789752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// an issue. This doesn't effect the type checker, since it operates
15799752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
1580d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
1581f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
1582f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
15839752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
15849752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
1585d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
1586d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTagDeclType(TagDecl *Decl) {
1590d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
15912ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  return getTypeDeclType(Decl);
15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// needs to agree with the definition in <stddef.h>.
15975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const {
1598b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
160164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
160264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
160364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
160464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
160564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
160664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
160764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
160864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
160964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
161064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
161164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
161264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
161364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
161464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
16158b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
16168b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
16178b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
1618b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
16198b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
16208b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
1621e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
1622e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
1623e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
1624e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
162577c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
162677c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
162777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
162877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
162977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
163077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
163177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris LattnerQualType ASTContext::getCanonicalType(QualType T) {
163277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner  QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
1633c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1634c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the result has type qualifiers, make sure to canonicalize them as well.
1635c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1636c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (TypeQuals == 0) return CanType;
1637c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1638c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If the type qualifiers are on an array type, get the canonical type of the
1639c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // array with the qualifiers applied to the element type.
1640c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
1641c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
1642c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return CanType.getQualifiedType(TypeQuals);
1643c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1644c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
1645c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
1646c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1647c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
1648c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1649c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1650c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1651c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                CAT->getIndexTypeQualifier());
1652c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1653c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1654c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                  IAT->getIndexTypeQualifier());
1655c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1656898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1657898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1658898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                      DSAT->getSizeModifier(),
1659898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                      DSAT->getIndexTypeQualifier());
1660898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1661c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
1662c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1663c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                              VAT->getSizeModifier(),
1664c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                              VAT->getIndexTypeQualifier());
1665c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
1666c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
16677da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas GregorDecl *ASTContext::getCanonicalDecl(Decl *D) {
1668c4ccf01c1223ea13efc5b6dd19ca4916af0daf6aDouglas Gregor  if (!D)
1669c4ccf01c1223ea13efc5b6dd19ca4916af0daf6aDouglas Gregor    return 0;
1670c4ccf01c1223ea13efc5b6dd19ca4916af0daf6aDouglas Gregor
16717da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
16727da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    QualType T = getTagDeclType(Tag);
16737da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
16747da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor                         ->getDecl());
16757da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  }
16767da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
16777da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
16787da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    while (Template->getPreviousDeclaration())
16797da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor      Template = Template->getPreviousDeclaration();
16807da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    return Template;
16817da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  }
16827da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
16837da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
16847da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    while (Function->getPreviousDeclaration())
16857da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor      Function = Function->getPreviousDeclaration();
16867da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    return const_cast<FunctionDecl *>(Function);
16877da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  }
16887da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
16897da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
16907da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    while (Var->getPreviousDeclaration())
16917da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor      Var = Var->getPreviousDeclaration();
16927da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    return const_cast<VarDecl *>(Var);
16937da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  }
16947da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
16957da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor  return D;
16967da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor}
16977da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor
169825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
169925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
170025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
170125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
17027da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor    return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
170325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
170425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
170525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
170625a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
170725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
170825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
1709d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
1710d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1711d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (!NNS)
1712d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
1713d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1714d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
1715d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
1716d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
1717d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this,
1718d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1719d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
1720d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1721d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
1722d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
1723d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
1724d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1725d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1726d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
1727d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
1728d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1729d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *Prefix = 0;
1730d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1731d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // FIXME: This isn't the right check!
1732d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (T->isDependentType())
1733d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1734d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1735d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, Prefix,
1736d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1737d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
1738d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1739d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1740d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
1741d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
1742d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
1743d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1744d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1745d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
1746d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
1747d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1748d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1749c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1750c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
1751c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
1752c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (T.getCVRQualifiers() == 0) {
1753c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
1754c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1755c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
1756c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
1757c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1758c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the common negative case fast, ignoring CVR qualifiers.
1759c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
1760c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1761f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Make sure to look through type qualifiers (like ExtQuals) for the negative
1762c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // test.
1763c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!isa<ArrayType>(CType) &&
1764c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      !isa<ArrayType>(CType.getUnqualifiedType()))
1765c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
1766c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1767c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Apply any CVR qualifiers from the array type to the element type.  This
1768c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
1769c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
1770c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1771c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
1772c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
1773c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // we must propagate them down into the elemeng type.
1774c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned CVRQuals = T.getCVRQualifiers();
1775c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  unsigned AddrSpace = 0;
1776c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  Type *Ty = T.getTypePtr();
1777c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1778f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian  // Rip through ExtQualType's and typedefs to get to a concrete type.
1779c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  while (1) {
1780f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1781f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      AddrSpace = EXTQT->getAddressSpace();
1782f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian      Ty = EXTQT->getBaseType();
1783c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    } else {
1784c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      T = Ty->getDesugaredType();
1785c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1786c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner        break;
1787c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      CVRQuals |= T.getCVRQualifiers();
1788c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      Ty = T.getTypePtr();
1789c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    }
1790c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
1791c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1792c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
1793c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1794c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1795c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
1796c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1797c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
1798c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
1799c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
1800c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
1801c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType NewEltTy = ATy->getElementType();
1802c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (AddrSpace)
1803f11284ac87daa613bc7b30db9f54bd716d123222Fariborz Jahanian    NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
1804c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1805c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1806c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1807c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1808c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
1809c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getIndexTypeQualifier()));
1810c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1811c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1812c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
1813c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                 IAT->getIndexTypeQualifier()));
1814898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1815898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (const DependentSizedArrayType *DSAT
1816898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
1817898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
1818898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                     getDependentSizedArrayType(NewEltTy,
1819898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeExpr(),
1820898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
1821898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getIndexTypeQualifier()));
1822c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
1823c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1824c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1825c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
1826c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getIndexTypeQualifier()));
182777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
182877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
182977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
1830e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
1831e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
1832e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
1833e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
1834e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
1835e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1836e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
1837c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
1838c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
1839c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
1840c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
1841c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1842c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
1843e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1844c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
1845e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1846e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
1847c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
1848e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
1849e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1850d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel DunbarQualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
18516183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  QualType ElemTy = VAT->getElementType();
18526183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
18536183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
18546183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson    return getBaseElementType(VAT);
18556183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
18566183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
18576183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
18586183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
18595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
1861a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
1862ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  if (const ComplexType *CT = T->getAsComplexType())
18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
1864a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
1865d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel Dunbar  assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
1866ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  switch (T->getAsBuiltinType()->getKind()) {
1867a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
18685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
18695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
18715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1874716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1875716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// point or a complex type (based on typeDomain/typeSize).
1876716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
1877716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
18781361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
18791361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
18801361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
18811361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
18821361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
1883716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
1884f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
1885f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
1886f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
1887f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
1888f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
18891361b11066239ea15764a2a844405352d87296b3Chris Lattner
18901361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
18911361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
18921361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
18931361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
18941361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
18951361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
19007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
19017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
19027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
1903a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1904a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
1905a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
1906a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
1907a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
1908fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
1909a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
1910fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
1911fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
19125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1914f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1915f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
1916f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
1917f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
1918f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  assert(T->isCanonical() && "T should be canonicalized");
1919f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
1920f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    T = ET->getDecl()->getIntegerType().getTypePtr();
1921f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
1922f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // There are two things which impact the integer rank: the width, and
1923f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // the ordering of builtins.  The builtin ordering is encoded in the
1924f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // bottom three bits; the width is encoded in the bits above that.
1925f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1926f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth() << 3;
1927f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  }
1928f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
1929f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
19307cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
19317cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
1932f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
19337cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
19347cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
19357cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
19367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
1937f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
19387cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
19397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
1940f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
19417cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
19427cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
1943f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
19447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
19457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
1946f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
19477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
19487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
1949f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
19502df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
19512df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
19522df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
1953f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
1954f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
1955f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
19567cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getIntegerTypeOrder - Returns the highest ranked integer type:
19577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
19587cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
19597cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
1960f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
1961f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
19627cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
19635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1964f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1965f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
19687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
19695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
19717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
19727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
19737cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
19747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
19757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
19767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
19777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
19787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
19797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
19807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
19817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
19827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
19837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // powers of two larger than each other, this is always safe.
19847cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
19857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
19867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
19877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
19887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
19897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
19905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
19927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
19937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // powers of two larger than each other, this is always safe.
19947cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
19955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
199671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
199771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson// getCFConstantStringType - Return the type used for constant CFStrings.
199871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
199971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
20006c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner    CFConstantStringTypeDecl =
200139ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2002df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek                         &Idents.get("NSConstantString"));
2003f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
200471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
200571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
200671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
2007f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2008f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
200971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
2010f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
201171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
2012f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[3] = LongTy;
201371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
201444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
201544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
201644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
201744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
201844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           FieldTypes[i], /*BitWidth=*/0,
20194afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
20206ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      CFConstantStringTypeDecl->addDecl(*this, Field);
202144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
202244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
202344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    CFConstantStringTypeDecl->completeDefinition(*this);
202471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
202571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
202671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
20278467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2028b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2029319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
2030319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const RecordType *Rec = T->getAsRecordType();
2031319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2032319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2033319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2034319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2035bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders CarlssonQualType ASTContext::getObjCFastEnumerationStateType()
2036bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson{
2037bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
203844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
203944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
204044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                         &Idents.get("__objcFastEnumerationState"));
204144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
2042bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2043bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2044bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(ObjCIdType),
2045bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2046bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2047bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2048bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
2049bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
205044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
205144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      FieldDecl *Field = FieldDecl::Create(*this,
205244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           ObjCFastEnumerationStateTypeDecl,
205344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
205444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           FieldTypes[i], /*BitWidth=*/0,
20554afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
20566ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
205744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
2058bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
205944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
2060bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
2061bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2062bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2063bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2064bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2065319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
2066319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const RecordType *Rec = T->getAsRecordType();
2067319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2068319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2069319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2070319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2071e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
2072e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
20732d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
2074e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
2075bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2076bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
207785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
207885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
207985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
208085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2081a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
208233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
2083a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) {
208498be4943e8dc4f3905629a7102668960873cf863Chris Lattner  uint64_t sz = getTypeSize(type);
208533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
208633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
208733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (sz > 0 && type->isIntegralType())
208898be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = std::max(sz, getTypeSize(IntTy));
208933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
209033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
209198be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = getTypeSize(VoidPtrTy);
209298be4943e8dc4f3905629a7102668960873cf863Chris Lattner  return sz / getTypeSize(CharTy);
209333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
209433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2095a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
209633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
2097c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
2098e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
2099c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2100ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
2101a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
210233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
21030d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
210433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
210533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
210633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
210733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
210898be4943e8dc4f3905629a7102668960873cf863Chris Lattner  int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
210933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
211033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
211133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  int ParmOffset = 2 * PtrSize;
211289951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
211389951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
211489951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
211589951a86b594513c2a013532ed45d197413b1087Chris Lattner    int sz = getObjCEncodingTypeSize(PType);
2116a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
211733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
211833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
211933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(ParmOffset);
212033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
212133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(PtrSize);
212233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
212333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
212433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
212589951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
212689951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
212789951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
21284306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    QualType PType = PVDecl->getOriginalType();
21294306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
2130ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2131ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
2132ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
2133bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
2134ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
2135ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
2136ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
2137ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
213833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
21394306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
21400d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
214133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    S += llvm::utostr(ParmOffset);
2142a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ParmOffset += getObjCEncodingTypeSize(PType);
214333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
214433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
214533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2146c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
214783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
2148c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2149c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
215083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// Property attributes are stored as a comma-delimited C string. The simple
215183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// attributes readonly and bycopy are encoded as single characters. The
215283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// parametrized attributes, getter=name, setter=name, and ivar=name, are
215383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// encoded as single characters, followed by an identifier. Property types
215483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// are also encoded as a parametrized attribute. The characters used to encode
215583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
215683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
215783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
215883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
215983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
216083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
216183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
216283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
216383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
216483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
216583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
216683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
216783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
216883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
216983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
217083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
2171c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbarvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2172c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
2173e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
2174c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
2175c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
2176c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
2177c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2178c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
2179c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
2180c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    if (const ObjCCategoryImplDecl *CID =
2181c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
2182c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
2183653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor             i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2184653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2185c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2186c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2187c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2188c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2189c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2190c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2191c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2192c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2193c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2194c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
219561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
2196c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
2197653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor             i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2198653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2199c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2200c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2201c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2202c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2203c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2204c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2205c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2206c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2207c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2208c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2209c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2210c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2211c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2212c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
2213c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2214c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
2215090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
2216090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
2217153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
2218090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
2219090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
2220c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2221c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
2222c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
2223c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
2224c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
2225c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
2226c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
2227c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Retain: S += ",&"; break;
2228c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2229c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2230c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2231c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
2232c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
2233c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
2234c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
2235c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2236090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2237090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
2238090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian
2239c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2240c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
2241077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
2242c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2243c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2244c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2245c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
2246077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
2247c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2248c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2249c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
2250c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2251c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
225239f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
2253c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2254c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2255c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
2256c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
2257c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2258a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
2259a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// Another legacy compatibility encoding: 32-bit longs are encoded as
2260c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian/// 'l' or 'L' , but not always.  For typedefs, we need to use
2261a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2262a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
2263a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2264a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2265a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
2266c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
2267c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2268a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
2269c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      else
2270c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
2271c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2272a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
2273a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2274a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
2275a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
2276a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
22777d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
2278153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
227982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
228082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
228182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
228282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
22835b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian  getObjCEncodingForTypeImpl(T, S, true, true, Field,
22845b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
228582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
228682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
22878b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanianstatic void EncodeBitField(const ASTContext *Context, std::string& S,
2288153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                           const FieldDecl *FD) {
22898b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
22908b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
22918b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
22929a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
22938b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
22948b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
22958b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
22968b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
229782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
229882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
229982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
2300153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
2301090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
23026ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
2303e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const BuiltinType *BT = T->getAsBuiltinType()) {
230443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    if (FD && FD->isBitField()) {
23058b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
230685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
230743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    else {
230843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      char encoding;
230943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      switch (BT->getKind()) {
231043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      default: assert(0 && "Unhandled builtin type kind");
231143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Void:       encoding = 'v'; break;
231243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Bool:       encoding = 'B'; break;
231343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Char_U:
231443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::UChar:      encoding = 'C'; break;
231543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::UShort:     encoding = 'S'; break;
231643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::UInt:       encoding = 'I'; break;
231772696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian      case BuiltinType::ULong:
231872696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian          encoding =
231972696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian            (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
232072696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian          break;
23212df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner      case BuiltinType::UInt128:    encoding = 'T'; break;
232243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::ULongLong:  encoding = 'Q'; break;
232343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Char_S:
232443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::SChar:      encoding = 'c'; break;
232543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Short:      encoding = 's'; break;
232643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Int:        encoding = 'i'; break;
232772696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian      case BuiltinType::Long:
232872696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        encoding =
232972696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian          (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
233072696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        break;
233143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::LongLong:   encoding = 'q'; break;
23322df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner      case BuiltinType::Int128:     encoding = 't'; break;
233343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Float:      encoding = 'f'; break;
233443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::Double:     encoding = 'd'; break;
233543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      case BuiltinType::LongDouble: encoding = 'd'; break;
233643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      }
233785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
233843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      S += encoding;
233943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
2340c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson  } else if (const ComplexType *CT = T->getAsComplexType()) {
2341c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
2342c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2343c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
2344c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson  } else if (T->isObjCQualifiedIdType()) {
2345090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    getObjCEncodingForTypeImpl(getObjCIdType(), S,
2346090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                               ExpandPointedToStructures,
2347090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                               ExpandStructures, FD);
2348090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    if (FD || EncodingProperty) {
2349090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      // Note that we do extended encoding of protocol qualifer list
2350090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      // Only when doing ivar or property encoding.
2351090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2352090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      S += '"';
2353090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2354090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian        ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2355090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian        S += '<';
2356090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian        S += Proto->getNameAsString();
2357090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian        S += '>';
2358090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      }
2359090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      S += '"';
2360090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    }
2361090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    return;
2362c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  }
2363c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  else if (const PointerType *PT = T->getAsPointerType()) {
236485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    QualType PointeeTy = PT->getPointeeType();
2365a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
2366a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
2367a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
2368a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2369a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // Also, do not emit the 'r' for anything but the outermost type!
2370a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (dyn_cast<TypedefType>(T.getTypePtr())) {
2371a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
2372a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2373a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2374a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2375a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2376a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    else if (OutermostType) {
2377a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
2378a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      while (P->getAsPointerType())
2379a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        P = P->getAsPointerType()->getPointeeType();
2380a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
2381a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2382a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2383a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2384a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2385a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
2386a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
2387a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
2388a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
2389a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      const char * s = S.c_str();
2390a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      int len = S.length();
2391a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2392a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        std::string replace = "rn";
2393a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S.replace(S.end()-2, S.end(), replace);
2394a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2395a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2396389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    if (isObjCIdStructType(PointeeTy)) {
2397c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian      S += '@';
2398c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian      return;
2399c166d7390c300e84a0001c016b83f8aa6de9cf53Fariborz Jahanian    }
2400c166d7390c300e84a0001c016b83f8aa6de9cf53Fariborz Jahanian    else if (PointeeTy->isObjCInterfaceType()) {
2401bb99bdec7d3e704485baffae85938d24d35f0e0fFariborz Jahanian      if (!EncodingProperty &&
2402225dfd7d6b0ed14e6b7ad8dc121161b9f34ecfceFariborz Jahanian          isa<TypedefType>(PointeeTy.getTypePtr())) {
24033e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian        // Another historical/compatibility reason.
24043e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian        // We encode the underlying type which comes out as
24053e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian        // {...};
24063e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian        S += '^';
24073e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian        getObjCEncodingForTypeImpl(PointeeTy, S,
24083e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian                                   false, ExpandPointedToStructures,
24093e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian                                   NULL);
24103e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian        return;
24113e1b16c2e5b2c02d76e60ff28d609eeb21a2eb71Fariborz Jahanian      }
2412c166d7390c300e84a0001c016b83f8aa6de9cf53Fariborz Jahanian      S += '@';
2413090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian      if (FD || EncodingProperty) {
241486f938b3d4a79fb9c337d351c8f3e5f1ff18f1b6Fariborz Jahanian        const ObjCInterfaceType *OIT =
241586f938b3d4a79fb9c337d351c8f3e5f1ff18f1b6Fariborz Jahanian                PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
2416090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian        ObjCInterfaceDecl *OI = OIT->getDecl();
2417adcaf544a9d863a4afb29cc5420095320fccafd8Fariborz Jahanian        S += '"';
2418adcaf544a9d863a4afb29cc5420095320fccafd8Fariborz Jahanian        S += OI->getNameAsCString();
2419090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian        for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2420090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian          ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2421090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian          S += '<';
2422090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian          S += Proto->getNameAsString();
2423090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian          S += '>';
2424090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian        }
2425adcaf544a9d863a4afb29cc5420095320fccafd8Fariborz Jahanian        S += '"';
2426adcaf544a9d863a4afb29cc5420095320fccafd8Fariborz Jahanian      }
2427c166d7390c300e84a0001c016b83f8aa6de9cf53Fariborz Jahanian      return;
2428389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    } else if (isObjCClassStructType(PointeeTy)) {
24298baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += '#';
24308baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
2431a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    } else if (isObjCSelType(PointeeTy)) {
24328baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += ':';
24338baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
2434c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian    }
243585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
243685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
243785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
243885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
2439e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
244085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
244185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
244285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
244385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
244485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
244585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
2446a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
2447a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
2448a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getObjCEncodingForTypeImpl(PointeeTy, S,
2449d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar                               false, ExpandPointedToStructures,
245043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
2451c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  } else if (const ArrayType *AT =
2452c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner               // Ignore type qualifiers etc.
2453c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner               dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
2454559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
2455559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
2456559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
2457559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
2458559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      getObjCEncodingForTypeImpl(AT->getElementType(), S,
2459559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
2460559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
2461559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
246285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2463559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2464559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
2465559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
2466559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
2467559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2468559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
2469559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
247085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2471559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      getObjCEncodingForTypeImpl(AT->getElementType(), S,
2472559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
2473559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
2474559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
2475c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson  } else if (T->getAsFunctionType()) {
2476c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
24776de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian  } else if (const RecordType *RTy = T->getAsRecordType()) {
247882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
2479d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
2480502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
2481502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2482502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
2483502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
2484502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
2485502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
24860d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
24877d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
24886ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor      for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
24896ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                   FieldEnd = RDecl->field_end(*this);
249044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
249143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
2492d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
249344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
2494d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
2495d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
2496d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar
2497d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
249843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
249943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
250043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
2501d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
2502a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
2503a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
2504a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getObjCEncodingForTypeImpl(qt, S, false, true,
250543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
2506d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
25077d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
25086de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
2509d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
25105e71124dabe8017f17ce8996e4161a202694e3e6Steve Naroff  } else if (T->isEnumeralType()) {
25118b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
25128b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
25138b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
25148b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
2515485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  } else if (T->isBlockPointerType()) {
251621a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
251743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  } else if (T->isObjCInterfaceType()) {
251843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
251943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
252043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
252143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
252243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
252343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
2524f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
252543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
2526f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
252743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
252843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
252943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
253043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
253143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
253243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
253343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
253443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
253543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
253643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  else
2537f69cc5d6606fc65a76e3acd6eb6e13efd0098295Steve Naroff    assert(0 && "@encode for type not implemented!");
253885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
253985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2540a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
2541ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
2542ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
2543ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
2544ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
2545ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
2546ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
2547ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
2548ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
2549ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
2550ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
2551ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
2552ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
2553ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
2554ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
2555ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
2556b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlssonvoid ASTContext::setBuiltinVaListType(QualType T)
2557b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson{
2558b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2559b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2560b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
2561b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
2562b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2563319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCIdType(QualType T)
25647e219e47de26346885d667131977bd9ca2d7662aSteve Naroff{
2565319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCIdType = T;
2566319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2567319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const TypedefType *TT = T->getAsTypedefType();
2568319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (!TT)
2569319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    return;
2570319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2571319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  TypedefDecl *TD = TT->getDecl();
25727e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
25737e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  // typedef struct objc_object *id;
25747e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2575c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  // User error - caller will issue diagnostics.
2576c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!ptr)
2577c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
25787e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2579c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  // User error - caller will issue diagnostics.
2580c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!rec)
2581c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
25827e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  IdStructType = rec;
25837e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
25847e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
2585319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCSelType(QualType T)
2586b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian{
2587319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCSelType = T;
2588319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2589319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const TypedefType *TT = T->getAsTypedefType();
2590319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (!TT)
2591319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    return;
2592319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  TypedefDecl *TD = TT->getDecl();
2593b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
2594b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  // typedef struct objc_selector *SEL;
2595b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2596c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!ptr)
2597c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
2598b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2599c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!rec)
2600c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
2601b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  SelStructType = rec;
2602b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
2603b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
2604a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCProtoType(QualType QT)
2605390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian{
2606a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
2607390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
2608390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
2609319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCClassType(QualType T)
26108baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson{
2611319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCClassType = T;
2612319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2613319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  const TypedefType *TT = T->getAsTypedefType();
2614319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (!TT)
2615319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    return;
2616319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  TypedefDecl *TD = TT->getDecl();
26178baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
26188baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  // typedef struct objc_class *Class;
26198baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
26208baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  assert(ptr && "'Class' incorrectly typed");
26218baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
26228baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  assert(rec && "'Class' incorrectly typed");
26238baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  ClassStructType = rec;
26248baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
26258baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
2626a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2627a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCConstantStringType.isNull() &&
26282198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
26292198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
2630a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
26312198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
26322198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
26337532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a qualified
26347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c std::vector.
26357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
26367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  bool TemplateKeyword,
26377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  TemplateDecl *Template) {
26387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
26397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
26407532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26417532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
26427532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName *QTN =
26437532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
26447532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (!QTN) {
26457532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
26467532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
26477532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
26487532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26497532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
26507532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
26517532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26527532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
26537532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
26547532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
26557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
26567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  assert(NNS->isDependent() && "Nested name specifier must be dependent");
26577532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
26597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
26607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
26627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
26637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
26647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
26667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
26677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
26697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
26707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
26717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
26727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
26737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
26747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
26757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
26767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
26777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
26787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
26797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
2680b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
2681d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
2682d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
2683d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas GregorQualType ASTContext::getFromTargetType(unsigned Type) const {
2684b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
2685b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::NoInt: return QualType();
2686b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
2687b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
2688b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
2689b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
2690b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
2691b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
2692b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
2693b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2694b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
2695b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
2696b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
2697b3ac5434ab936f092b8cc48349cb01db3a1e1c76Daniel Dunbar  return QualType();
2698b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
2699b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
2700b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
2701b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
2702b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
2703b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
2704fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
2705fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
2706fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
2707fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
2708fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
2709fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2710fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
2711fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian      if (TD->getAttr<ObjCNSObjectAttr>())
2712fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
2713fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
2714fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  return false;
2715fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
2716fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
2717b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2718b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek/// to an object type.  This includes "id" and "Class" (two 'special' pointers
2719b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2720b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek/// ID type).
2721b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenekbool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2722d461777e23204fe8c480302d8ff76f5847605da6Steve Naroff  if (Ty->isObjCQualifiedIdType())
2723b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek    return true;
2724b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
27256ae98506f93617b1eda486406b376c9b70cd2df3Steve Naroff  // Blocks are objects.
27266ae98506f93617b1eda486406b376c9b70cd2df3Steve Naroff  if (Ty->isBlockPointerType())
27276ae98506f93617b1eda486406b376c9b70cd2df3Steve Naroff    return true;
27286ae98506f93617b1eda486406b376c9b70cd2df3Steve Naroff
27296ae98506f93617b1eda486406b376c9b70cd2df3Steve Naroff  // All other object types are pointers.
273016ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  const PointerType *PT = Ty->getAsPointerType();
273116ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  if (PT == 0)
2732b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek    return false;
2733b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
2734b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek  // If this a pointer to an interface (e.g. NSString*), it is ok.
273516ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  if (PT->getPointeeType()->isObjCInterfaceType() ||
273616ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner      // If is has NSObject attribute, OK as well.
273716ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner      isObjCNSObjectType(Ty))
2738fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    return true;
2739fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
274016ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  // Check to see if this is 'id' or 'Class', both of which are typedefs for
274116ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  // pointer types.  This looks for the typedef specifically, not for the
274216ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  // underlying type.  Iteratively strip off typedefs so that we can handle
274316ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  // typedefs of typedefs.
274416ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
274516ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner    if (Ty.getUnqualifiedType() == getObjCIdType() ||
274616ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner        Ty.getUnqualifiedType() == getObjCClassType())
274716ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner      return true;
274816ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner
274916ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner    Ty = TDT->getDecl()->getUnderlyingType();
275016ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  }
275116ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner
275216ede0ec39bf72f5e696e8ed5f1533b29ac2f3d8Chris Lattner  return false;
2753b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek}
2754b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
27554fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
27564fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
27574fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
27584fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz JahanianQualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
2759b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  QualType::GCAttrTypes GCAttrs = QualType::GCNone;
27604fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
27614fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
2762b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
27634fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
2764a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // (or pointers to them) be treated as though they were declared
2765a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
2766a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    if (GCAttrs == QualType::GCNone) {
2767a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      if (isObjCObjectPointerType(Ty))
2768a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian        GCAttrs = QualType::Strong;
2769a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
2770a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian        return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2771a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
2772c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
2773c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
2774c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2775c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian      return QualType::GCNone;
27764fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
2777b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
27784fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
27794fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
27806ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
27816ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
27826ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
2783770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
27841c7d067550c2d9bca8997d0e67ee6b280d493202Steve Naroff/// typesAreBlockCompatible - This routine is called when comparing two
2785dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff/// block types. Types must be strictly compatible here. For example,
2786dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff/// C unfortunately doesn't produce an error for the following:
2787dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///
2788dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///   int (*emptyArgFunc)();
2789dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///   int (*intArgList)(int) = emptyArgFunc;
2790dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///
2791dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff/// For blocks, we will produce an error for the following (similar to C++):
2792dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///
2793dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///   int (^emptyArgBlock)();
2794dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///   int (^intArgBlock)(int) = emptyArgBlock;
2795dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///
2796dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2797dd972f20dc2bd3609d833893e5c6544ac09b59a9Steve Naroff///
27981c7d067550c2d9bca8997d0e67ee6b280d493202Steve Naroffbool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
2799c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  const FunctionType *lbase = lhs->getAsFunctionType();
2800c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  const FunctionType *rbase = rhs->getAsFunctionType();
280172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
280272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
2803aab0f7a749e939d1d485a436f57ae56d14ba3016Mike Stump  if (lproto && rproto == 0)
2804aab0f7a749e939d1d485a436f57ae56d14ba3016Mike Stump    return false;
2805aab0f7a749e939d1d485a436f57ae56d14ba3016Mike Stump  return !mergeTypes(lhs, rhs).isNull();
28061c7d067550c2d9bca8997d0e67ee6b280d493202Steve Naroff}
28071c7d067550c2d9bca8997d0e67ee6b280d493202Steve Naroff
28086ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// areCompatVectorTypes - Return true if the two specified vector types are
28096ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
28106ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
28116ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
28126ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  assert(LHS->isCanonical() && RHS->isCanonical());
28136ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
281461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
28156ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
28166ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
28173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman/// canAssignObjCInterfaces - Return true if the two interface types are
28186ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible for assignment from RHS to LHS.  This handles validation of any
28196ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// protocol qualifiers on the LHS or RHS.
28206ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner///
28213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
28223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman                                         const ObjCInterfaceType *RHS) {
28236ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
28246ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
28256ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
28266ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
28276ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
28286ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
28296ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
28306ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!isa<ObjCQualifiedInterfaceType>(LHS))
28316ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
28326ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
28336ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
28346ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
28356ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!isa<ObjCQualifiedInterfaceType>(RHS))
28366ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
28376ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
28386ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Finally, we must have two protocol-qualified interfaces.
28396ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
28406ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
28416ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
284291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // All LHS protocols must have a presence on the RHS.
284391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
284491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
284591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
284691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff                                                 LHSPE = LHSP->qual_end();
284791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
284891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
284991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
285091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
285191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
285291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
285391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff                                                   RHSPE = RHSP->qual_end();
285491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff         !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
285591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
285691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
285791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
285891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
285991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
286091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
286191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
286291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
286391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
28646ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
28656ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
2866389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2867389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
2868389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  const PointerType *LHSPT = LHS->getAsPointerType();
2869389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  const PointerType *RHSPT = RHS->getAsPointerType();
2870389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
2871389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  if (!LHSPT || !RHSPT)
2872389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
2873389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
2874389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  QualType lhptee = LHSPT->getPointeeType();
2875389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  QualType rhptee = RHSPT->getPointeeType();
2876389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2877389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2878389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // ID acts sort of like void* for ObjC interfaces
2879389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  if (LHSIface && isObjCIdStructType(rhptee))
2880389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return true;
2881389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  if (RHSIface && isObjCIdStructType(lhptee))
2882389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return true;
2883389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  if (!LHSIface || !RHSIface)
2884389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
2885389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2886389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff         canAssignObjCInterfaces(RHSIface, LHSIface);
2887389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
2888389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
2889ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2890ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
2891ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// C99 6.2.7p1: Two types have compatible types if their types are the
2892ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
28933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
28943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
28953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
28963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
28973d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
28983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *lbase = lhs->getAsFunctionType();
28993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  const FunctionType *rbase = rhs->getAsFunctionType();
290072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
290172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
29023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
29033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
29043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
29063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
29073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
290861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
290961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
291061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
291161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
29123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
29143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
29153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
29163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
29183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
29193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
29203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
29223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
29233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
29243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29257fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
29267fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
29277fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
29283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
29293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
29303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
29313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
29323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
29333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argtype = mergeTypes(largtype, rargtype);
29343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
29353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
293661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
293761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
293861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
293961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
29403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
29413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
29423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
29433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
29447fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                           lproto->isVariadic(), lproto->getTypeQuals());
29453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
29463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
29483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
29493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
295072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
29513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
29523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
29533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
29543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
29553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
29563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
29573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
29583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
29593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
29603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
29613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
29623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
29633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
29643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
29653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
29673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
29683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
29697fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                           proto->getNumArgs(), lproto->isVariadic(),
29707fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis                           lproto->getTypeQuals());
29713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
29723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
29743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
297572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return getFunctionNoProtoType(retType);
29763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
29773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29783d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
297943d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
298043d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
298143d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
29827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
29837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
29843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // FIXME: C++ shouldn't be going through here!  The rules are different
29853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // enough that they should be handled separately.
29867c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
29877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // shouldn't be going through here!
29883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (const ReferenceType *RT = LHS->getAsReferenceType())
2989c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    LHS = RT->getPointeeType();
29903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (const ReferenceType *RT = RHS->getAsReferenceType())
2991c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    RHS = RT->getPointeeType();
29923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
29933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
29943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
29953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
2996f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
29973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
29983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
29993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
30003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // If the qualifiers are different, the types aren't compatible
30015a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // Note that we handle extended qualifiers later, in the
30025a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  // case for ExtQualType.
30035a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
30043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
30053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3006c8d2e77288c07a69cabb755a4ab9feeb7b5a1287Fariborz Jahanian  Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3007c8d2e77288c07a69cabb755a4ab9feeb7b5a1287Fariborz Jahanian  Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
3008f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
30091adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
30101adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
30111adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
30121adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
30134c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
30144c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
3015a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3016a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
3017a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3018a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
3019a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner
3020213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
3021213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3022213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
3023ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
3024b0489814dcb3cb801eeb0784cc6b6fcf575ff71dChris Lattner  // Consider qualified interfaces and interfaces the same.
3025b0489814dcb3cb801eeb0784cc6b6fcf575ff71dChris Lattner  if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3026b0489814dcb3cb801eeb0784cc6b6fcf575ff71dChris Lattner  if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
30273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
30284e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
30294e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
30305fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
30315fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3032c8d2e77288c07a69cabb755a4ab9feeb7b5a1287Fariborz Jahanian
3033d824c9c281c163ba86f3cc10c5572120234a2454Steve Naroff    // 'id' and 'Class' act sort of like void* for ObjC interfaces
3034d824c9c281c163ba86f3cc10c5572120234a2454Steve Naroff    if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
30355fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
3036d824c9c281c163ba86f3cc10c5572120234a2454Steve Naroff    if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
30375fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return RHS;
30385fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
3039bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    // ID is compatible with all qualified id types.
3040bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    if (LHS->isObjCQualifiedIdType()) {
3041bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff      if (const PointerType *PT = RHS->getAsPointerType()) {
3042bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        QualType pType = PT->getPointeeType();
3043d824c9c281c163ba86f3cc10c5572120234a2454Steve Naroff        if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
3044bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff          return LHS;
3045bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3046bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // Unfortunately, this API is part of Sema (which we don't have access
3047bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // to. Need to refactor. The following check is insufficient, since we
3048bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // need to make sure the class implements the protocol.
3049bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        if (pType->isObjCInterfaceType())
3050bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff          return LHS;
3051bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff      }
3052bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    }
3053bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    if (RHS->isObjCQualifiedIdType()) {
3054bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff      if (const PointerType *PT = LHS->getAsPointerType()) {
3055bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        QualType pType = PT->getPointeeType();
3056d824c9c281c163ba86f3cc10c5572120234a2454Steve Naroff        if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
3057bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff          return RHS;
3058bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3059bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // Unfortunately, this API is part of Sema (which we don't have access
3060bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // to. Need to refactor. The following check is insufficient, since we
3061bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        // need to make sure the class implements the protocol.
3062bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff        if (pType->isObjCInterfaceType())
3063bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff          return RHS;
3064bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff      }
3065bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    }
30661adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
30671adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // a signed integer type, or an unsigned integer type.
30683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = LHS->getAsEnumType()) {
30693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
30703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
3071bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
30723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (const EnumType* ETy = RHS->getAsEnumType()) {
30733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
30743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
3075bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
30761adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
30773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
3078ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
30793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
30804a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
30811adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
308272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
308372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
308472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
308572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
308672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
308772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
308872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
308972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
30907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
30917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
309272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
309372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
309472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
309572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
309672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
309772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
309872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
309972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
310072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ObjCQualifiedInterface:
310172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
310272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
310372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
31041adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
31053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
31063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
31073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
31083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
31093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
31103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
311161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
311261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
311361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
311461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
31153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
31163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
3117c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
3118c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
3119c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
3120c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3121c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3122c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3123c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
3124c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3125c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
3126c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3127c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
3128c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
3129c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
31301adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
31313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
31323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
31333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
31343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
31353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
31363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
31373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
31383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
31393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
31403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
314161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
314261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
314361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
314461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
31453bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
31463bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
31473bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
31483bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
31493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
31503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
315161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
315261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
315361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
315461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
31553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
31563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
31573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
31583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
31593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
31603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
31613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
31623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
31633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
31643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
31653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
31663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
31673bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
31683bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
316961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
31703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
31711adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
31723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return mergeFunctionTypes(LHS, RHS);
317372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
317472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
31753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // FIXME: Why are these compatible?
3176389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3177389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
31783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
31791adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
31803cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
31813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
318264cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
318364cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
318464cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
31853cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
31865a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
31873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
31883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
318961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
319061490e9a965cfee8a78c12c6802138844f04250dCedric Venet  case Type::ObjCInterface: {
31915fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    // Check if the interfaces are assignment compatible.
31925a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
31935a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
31945fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
31955fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
31965fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    if (LHSIface && RHSIface &&
31975fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff        canAssignObjCInterfaces(LHSIface, RHSIface))
31985fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
31995fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
32003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
320161490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
3202bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff  case Type::ObjCQualifiedId:
3203bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    // Distinct qualified id's are not compatible.
3204bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
32055a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::FixedWidthInt:
32065a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // Distinct fixed-width integers are not compatible.
32075a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
32085a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::ExtQual:
32095a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: ExtQual types can be compatible even if they're not
32105a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // identical!
32115a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
32125a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // First attempt at an implementation, but I'm not really sure it's
32135a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // right...
32145a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#if 0
32155a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* LQual = cast<ExtQualType>(LHSCan);
32165a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ExtQualType* RQual = cast<ExtQualType>(RHSCan);
32175a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
32185a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman        LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
32195a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return QualType();
32205a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
32215a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    LHSBase = QualType(LQual->getBaseType(), 0);
32225a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    RHSBase = QualType(RQual->getBaseType(), 0);
32235a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = mergeTypes(LHSBase, RHSBase);
32245a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (ResultType.isNull()) return QualType();
32255a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
32265a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (LHSCan.getUnqualifiedType() == ResCanUnqual)
32275a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return LHS;
32285a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    if (RHSCan.getUnqualifiedType() == ResCanUnqual)
32295a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman      return RHS;
32305a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
32315a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
32325a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
32335a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return ResultType;
32345a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman#endif
32357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  case Type::TemplateSpecialization:
32377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    assert(false && "Dependent types have no size");
32387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
3239ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
324072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
324172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
3242ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
32437192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
32445426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
3245ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
3246ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
324788054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
3248ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
3249ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (T == BoolTy)
3250ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
3251f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3252f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth();
3253f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  }
3254f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
3255ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
3256ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
3257ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
3258ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3259ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
3260ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (const EnumType* ETy = T->getAsEnumType())
3261ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
3262ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  const BuiltinType* BTy = T->getAsBuiltinType();
3263ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert (BTy && "Unexpected signed integer type");
3264ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
3265ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
3266ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
3267ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
3268ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
3269ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
3270ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
3271ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
3272ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
3273ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
3274ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
3275ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
32762df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
32772df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
3278ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
3279ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
3280ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
3281ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
3282ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
3283ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
32842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
32852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
32862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
3287