ASTContext.cpp revision 0953e767ff7817f97b3ab20896b229891eeff45b
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
1549aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
17aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
18b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
19e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2119cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
221b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
23a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
2585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
266fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
27557c5b1717bc8919b1b40cf2064b51491ec53a44Chris Lattner#include "llvm/Support/MemoryBuffer.h"
2829445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson#include "RecordLayoutBuilder.h"
2929445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3661710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
3761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner                       TargetInfo &t,
38e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
391b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                       bool FreeMem, unsigned size_reserve) :
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
42782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
470f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCIdRedefinitionType = QualType();
480f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCClassRedefinitionType = QualType();
491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
50e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
5114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
52e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
53e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Deallocate all the types.
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!Types.empty()) {
574b05b1dee6cc65ae61d93dab7edff72710f24589Ted Kremenek    Types.back()->Destroy(*this);
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Types.pop_back();
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
60b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman
61b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    llvm::FoldingSet<ExtQuals>::iterator
630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      I = ExtQualNodes.begin(), E = ExtQualNodes.end();
640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    while (I != E)
650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Deallocate(&*I++);
660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  {
69b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
70b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
71b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
72b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
73b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
74b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
75b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
76b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
77b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  {
78b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar    llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
79b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar      I = ObjCLayouts.begin(), E = ObjCLayouts.end();
80b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    while (I != E) {
81b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
82b74668edbc119880eb0a7e563432314432cb775dNuno Lopes      delete R;
83b74668edbc119880eb0a7e563432314432cb775dNuno Lopes    }
84b74668edbc119880eb0a7e563432314432cb775dNuno Lopes  }
85b74668edbc119880eb0a7e563432314432cb775dNuno Lopes
86ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  // Destroy nested-name-specifiers.
871ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor  for (llvm::FoldingSet<NestedNameSpecifier>::iterator
881ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor         NNS = NestedNameSpecifiers.begin(),
891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         NNSEnd = NestedNameSpecifiers.end();
901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump       NNS != NNSEnd;
911ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor       /* Increment in loop */)
921ae0afaf14b7ce9bed33c1fe51077d01a313434dDouglas Gregor    (*NNS++).Destroy(*this);
93ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
94ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  if (GlobalNestedNameSpecifier)
95ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor    GlobalNestedNameSpecifier->Destroy(*this);
96ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
97b26153c2b06934b6d39886cae2a379988d9c3e2bEli Friedman  TUDecl->Destroy(*this);
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
1022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
1032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
1087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
109dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
111dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
112dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
113dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
114dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
115c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
118dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
121dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
122dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
123dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
124dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
125dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
126dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
127dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
128dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
129dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
131dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
1322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
1342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
1352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
1362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
141f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
15315b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
17564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
1762df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
1772df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
1782df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
1792df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
1803a2503227c3db04a3619735127483263c1075ef7Chris Lattner  if (LangOpts.CPlusPlus) // C++ 3.9.1p5
1813a2503227c3db04a3619735127483263c1075ef7Chris Lattner    InitBuiltinType(WCharTy,           BuiltinType::WChar);
1823a2503227c3db04a3619735127483263c1075ef7Chris Lattner  else // C99
1833a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
18464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
185f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
186f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
187f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
188f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
189f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
190f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
191f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
192f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
193f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
194f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
1958e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor  // Placeholder type for functions.
196898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
197898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
198898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
199898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
200898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
201898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
202898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
203898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
2048e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Placeholder type for C++0x auto declarations whose real type has
206e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  // not yet been deduced.
207e89d15944dd3be750a09805ad21222d2fa9321faAnders Carlsson  InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
2081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
2138e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
2147e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
216de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
217de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
218de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
220de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // Builtin types for 'id' and 'Class'.
221de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
222de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
22314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
224a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
22733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
2286e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
2296e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
2306e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorVarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) {
2347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
2357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos
2367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
2377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
2387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
2391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
2417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) {
2457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
2467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
2477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
2487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
2497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  InstantiatedFromStaticDataMember[Inst] = Tmpl;
2507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
2517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
2520d8df780aef1acda5962347a32591efc629b6748Anders CarlssonUnresolvedUsingDecl *
2530d8df780aef1acda5962347a32591efc629b6748Anders CarlssonASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
2541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
2550d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    = InstantiatedFromUnresolvedUsingDecl.find(UUD);
2560d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
2570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
2600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
2610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
2620d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
2630d8df780aef1acda5962347a32591efc629b6748Anders CarlssonASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
2640d8df780aef1acda5962347a32591efc629b6748Anders Carlsson                                                   UnresolvedUsingDecl *UUD) {
2650d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
2660d8df780aef1acda5962347a32591efc629b6748Anders Carlsson         "Already noted what using decl what instantiated from");
2670d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
2680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
2690d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
270d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
271d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
272d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
273d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
274d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
2751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
277d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
278d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
279d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
280d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
281d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
282d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
283d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
284d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
2851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
286d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
287d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
288d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
2892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregornamespace {
2901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  class BeforeInTranslationUnit
2912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    : std::binary_function<SourceRange, SourceRange, bool> {
2922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    SourceManager *SourceMgr;
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  public:
2952e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    bool operator()(SourceRange X, SourceRange Y) {
2982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
2992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
3002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  };
3012e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
3022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Determine whether the given comment is a Doxygen-style comment.
3042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
3052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Start the start of the comment text.
3062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
3072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param End the end of the comment text.
3082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor///
3092e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \param Member whether we want to check whether this is a member comment
3102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
3112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// we only return true when we find a non-member comment.
3121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool
3131eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpisDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
3142e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                 bool Member = false) {
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const char *BufferStart
3162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
3172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
3182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
3191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (End - Start < 4)
3212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
3222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  assert(Start[0] == '/' && "Not a comment?");
3242e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
3252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
3262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
3272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return false;
3282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return (Start[3] == '<') == Member;
3302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
3312e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// \brief Retrieve the comment associated with the given declaration, if
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// it has one.
3342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorconst char *ASTContext::getCommentForDecl(const Decl *D) {
3352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!D)
3362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check whether we have cached a comment string for this declaration
3392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // already.
3401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::DenseMap<const Decl *, std::string>::iterator Pos
3412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = DeclComments.find(D);
3422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Pos != DeclComments.end())
3432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return Pos->second.c_str();
3442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If we have an external AST source and have not yet loaded comments from
3462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // that source, do so now.
3472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (ExternalSource && !LoadedExternalComments) {
3482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::vector<SourceRange> LoadedComments;
3492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ExternalSource->ReadComments(LoadedComments);
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!LoadedComments.empty())
3522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      Comments.insert(Comments.begin(), LoadedComments.begin(),
3532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                      LoadedComments.end());
3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3552e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    LoadedExternalComments = true;
3562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
3571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If there are no comments anywhere, we won't find anything.
3592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (Comments.empty())
3602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the declaration doesn't map directly to a location in a file, we
3632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // can't find the comment.
3642e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  SourceLocation DeclStartLoc = D->getLocStart();
3652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
3662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
3672e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
3682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Find the comment that occurs just before this declaration.
3692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator LastComment
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = std::lower_bound(Comments.begin(), Comments.end(),
3712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       SourceRange(DeclStartLoc),
3722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                       BeforeInTranslationUnit(&SourceMgr));
3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the location for the start of the declaration and find the
3752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // beginning of the file buffer.
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  std::pair<FileID, unsigned> DeclStartDecomp
3772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(DeclStartLoc);
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const char *FileBufferStart
3792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getBufferData(DeclStartDecomp.first).first;
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // First check whether we have a comment for a member.
3822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment != Comments.end() &&
3832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
3842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      isDoxygenComment(SourceMgr, *LastComment, true)) {
3852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> LastCommentEndDecomp
3862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(LastComment->getEnd());
3872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
3882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor        SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
3902e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                                     LastCommentEndDecomp.second)) {
3912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // The Doxygen member comment comes after the declaration starts and
3922e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is on the same line and in the same file as the declaration. This
3932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      // is the comment we want.
3942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      std::string &Result = DeclComments[D];
3951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Result.append(FileBufferStart +
3961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      SourceMgr.getFileOffset(LastComment->getBegin()),
3972e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                    FileBufferStart + LastCommentEndDecomp.second + 1);
3982e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      return Result.c_str();
3992e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4002e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4022e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (LastComment == Comments.begin())
4032e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
4042e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  --LastComment;
4052e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
4062e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Decompose the end of the comment.
4072e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::pair<FileID, unsigned> LastCommentEndDecomp
4082e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getDecomposedLoc(LastComment->getEnd());
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4102e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment and the declaration aren't in the same file, then they
4112e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // aren't related.
4122e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (DeclStartDecomp.first != LastCommentEndDecomp.first)
4132e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Check that we actually have a Doxygen comment.
4162e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (!isDoxygenComment(SourceMgr, *LastComment))
4172e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Compute the starting line for the declaration and for the end of the
4202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // comment (this is expensive).
4211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned DeclStartLine
4222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
4232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  unsigned CommentEndLine
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
4252e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                              LastCommentEndDecomp.second);
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4272e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // If the comment does not end on the line prior to the declaration, then
4282e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // the comment is not associated with the declaration at all.
4292e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  if (CommentEndLine + 1 != DeclStartLine)
4302e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    return 0;
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4322e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // We have a comment, but there may be more comments on the previous lines.
4332e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Keep looking so long as the comments are still Doxygen comments and are
4342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // still adjacent.
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned ExpectedLine
4362e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
4372e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<SourceRange>::iterator FirstComment = LastComment;
4382e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != Comments.begin()) {
4392e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Look at the previous comment
4402e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    --FirstComment;
4412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> Decomp
4422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this previous comment is in a different file, we're done.
4452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Decomp.first != DeclStartDecomp.first) {
4462e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4472e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4482e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If this comment is not a Doxygen comment, we're done.
4512e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (!isDoxygenComment(SourceMgr, *FirstComment)) {
4522e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4532e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4542e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4562e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // If the line number is not what we expected, we're done.
4572e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
4582e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    if (Line != ExpectedLine) {
4592e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      ++FirstComment;
4602e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      break;
4612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    }
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4632e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    // Set the next expected line number.
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ExpectedLine
4652e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
4662e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4682e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // The iterator range [FirstComment, LastComment] contains all of the
4692e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // BCPL comments that, together, are associated with this declaration.
4702e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Form a single comment block string for this declaration that concatenates
4712e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // all of these comments.
4722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::string &Result = DeclComments[D];
4732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  while (FirstComment != LastComment) {
4742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompStart
4752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
4762e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    std::pair<FileID, unsigned> DecompEnd
4772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor      = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
4782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    Result.append(FileBufferStart + DecompStart.second,
4792e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                  FileBufferStart + DecompEnd.second + 1);
4802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor    ++FirstComment;
4812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  }
4821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  // Append the last comment line.
4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Result.append(FileBufferStart +
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                  SourceMgr.getFileOffset(LastComment->getBegin()),
4862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor                FileBufferStart + LastCommentEndDecomp.second + 1);
4872e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  return Result.c_str();
4882e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor}
4892e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor
490464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
491464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
492464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
493a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
494b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
495b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
496b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
497183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
498b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
499b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
500b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
501b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
502b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
503b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
504b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
505b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
506b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
507196efbf095d75180305a45f3033caa1003746604Mike Stump/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
508af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
509af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
510b7d0844c887a72064b624dc6df12cbe1441f69d0Daniel Dunbarunsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
511dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
512dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
51340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
514dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    Align = std::max(Align, AA->getAlignment());
515dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
516af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
517af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
5186217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
5194cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      unsigned AS = RT->getPointeeType().getAddressSpace();
520f0930235ce58a91aa3b840bece9052f44d630536Anders Carlsson      Align = Target.getPointerAlign(AS);
5214cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson    } else if (!T->isIncompleteType() && !T->isFunctionType()) {
5224cc2cfd48d7c2d11141871cad590db7b52ce00a0Anders Carlsson      // Incomplete or function types default to 1.
523dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
524dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman        T = cast<ArrayType>(T)->getElementType();
525dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
526dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
527dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
528af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
529dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
530dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  return Align / Target.getCharWidth();
531af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
532b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
533a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
534a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
5350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
5360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
5370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
5380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
539d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
5401d75118af76cae2bfc06389cde410e14bd0a19fcDaniel DunbarASTContext::getTypeInfo(const Type *T) {
5415e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
5425e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
543a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
54472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
54572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
54618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
54772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
54872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
54918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
55072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
55172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5525d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
5535d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
55418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
55518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
55618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
55718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
55818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
55972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
560fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
56118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
56218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
56318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
56418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithExpr:
5667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  case Type::ConstantArrayWithoutExpr:
567fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
5681d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
5719e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
572030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
573030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
5745c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
575213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
576030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
57898be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<VectorType>(T)->getElementType());
5799e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
5804bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
5816fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
5826fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
5836fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // FIXME: this should probably be a target property.
5846fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    Align = 1 << llvm::Log2_32_Ceil(Align);
585030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
586030d8846c7e520330007087e949f621989876e3aChris Lattner  }
5875d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
5889e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
589a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
590692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
591d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
59218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
59318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
59418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
59518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
59618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
5976f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
5989e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
5999e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
6006f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
601692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
602692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
603692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
6046f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
6059e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
6069e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
6076f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
60864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis    case BuiltinType::WChar:
60964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
61064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
61164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
612f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
613f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
614f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
615f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
616f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
617f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
618f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
619f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
620692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
6216f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
6229e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
6239e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
6246f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
625692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
6266f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
6279e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
6289e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
6296f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
630692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
6316f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
6329e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
6339e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
6346f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
635692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
6366f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
6379e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
6389e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
6396f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
640ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
641ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
642ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
643ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
644ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
6456f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
6469e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
6479e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
6486f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
6505426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
6515426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
6526f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6536f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
6549e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
6559e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
6566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
6576e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
6586e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
6596e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
6601590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
661a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
662bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
663f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  case Type::FixedWidthInt:
664f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // FIXME: This isn't precisely correct; the width/alignment should depend
665f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    // on the available types for the target
666f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Width = cast<FixedWidthIntType>(T)->getWidth();
667736166b38235cf6d0ffb67638960d95fb2afcbd6Chris Lattner    Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
668f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Align = Width;
669f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    break;
670d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
6715426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
672f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
6736f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
674485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
675485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
676485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
677485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
678485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
679485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
680f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
681f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
6825426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
683f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
684f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
685f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
6867c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
6877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
6887ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner    // "When applied to a reference or a reference type, the result is the size
6895d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // of the referenced type." C++98 5.3.3p2: expr.sizeof.
6906f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // FIXME: This is wrong for struct layout: a reference in a struct has
6916f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // pointer size.
692bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
693f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
6941cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
6951cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
6961cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    // If we ever want to support other ABIs this needs to be abstracted.
6971cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson
698f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
7001cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
7011cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Width = PtrDiffInfo.first;
702f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    if (Pointee->isFunctionType())
703f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      Width *= 2;
7041cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
7051cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
706f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
7075d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
7085d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
7095d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
71198be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
7129e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
7135d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
7145d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
7155d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
71644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
7171d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
71844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
71944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Width = Layout.getSize();
72044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    Align = Layout.getAlignment();
72144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
72244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
72372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
72472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
7251d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
7261d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
7271d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
7288389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Width = 1;
7298389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      Align = 1;
7308389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
7318389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7331d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
7347176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
7357176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
7361d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
7377176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
7387176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
7397176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
740dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
741a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
7427532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
7437da2431c23ef1ee8acb114e39692246e1801afc2John McCall  case Type::Elaborated: {
7447da2431c23ef1ee8acb114e39692246e1801afc2John McCall    return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
7457da2431c23ef1ee8acb114e39692246e1801afc2John McCall  }
7467da2431c23ef1ee8acb114e39692246e1801afc2John McCall
74718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
74818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
74940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
75018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = Aligned->getAlignment();
75118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
75218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    } else
75318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
7547532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
7557176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
75618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
75718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
75818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
75918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
76018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
76118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
76218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
76318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
764395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
765395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
766395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
767395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
76818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::QualifiedName:
76918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
7701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TemplateSpecialization:
7721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
77318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
77418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // FIXME: this is likely to be wrong once we support template
77518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // aliases, since a template alias could refer to a typedef that
77618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // has an __aligned__ attribute on it.
77718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(getCanonicalType(T));
77818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
780464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
7819e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
782a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
783a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
78434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
78534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
78634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
78734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
78834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattnerunsigned ASTContext::getPreferredTypeAlign(const Type *T) {
78934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
7901eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
7911eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
792183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
7931eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
7941eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
7951eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
7961eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
7971eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
79834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
79934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
80034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
801a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarstatic void CollectLocalObjCIvars(ASTContext *Ctx,
802a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  const ObjCInterfaceDecl *OI,
803a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                                  llvm::SmallVectorImpl<FieldDecl*> &Fields) {
804a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
805a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian       E = OI->ivar_end(); I != E; ++I) {
806f1690858344968358131f8d5690d9ee458883000Chris Lattner    ObjCIvarDecl *IVDecl = *I;
807a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian    if (!IVDecl->isInvalidDecl())
808a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian      Fields.push_back(cast<FieldDecl>(IVDecl));
809a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian  }
810a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian}
811a769c004a2874504c17ea8afccbc4ad35fc33c9fFariborz Jahanian
812a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbarvoid ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
813a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar                             llvm::SmallVectorImpl<FieldDecl*> &Fields) {
814a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
815a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar    CollectObjCIvars(SuperClass, Fields);
816a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar  CollectLocalObjCIvars(this, OI, Fields);
817a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar}
818a80a0f6398df06c018af779a7ca82a29172c45d1Daniel Dunbar
8198e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
8208e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
8218e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
8228e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
8238e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
8248e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian                                 bool CollectSynthesized) {
8258e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
8268e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian         E = OI->ivar_end(); I != E; ++I) {
8278e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian     Ivars.push_back(*I);
8288e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8298e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  if (CollectSynthesized)
8308e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    CollectSynthesizedIvars(OI, Ivars);
8318e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8328e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8339820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
8349820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
83517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
83617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
8379820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
8389820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8409820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into nested protocols.
8419820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
8429820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
8439820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(*P, Ivars);
8449820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
8459820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
8469820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// CollectSynthesizedIvars -
8479820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian/// This routine collect synthesized ivars for the designated class.
8489820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
8499820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanianvoid ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
8509820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian                                llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
85117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
85217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8539820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
8549820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian      Ivars.push_back(Ivar);
8559820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8569820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // Also look into interface's protocol list for properties declared
8579820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  // in the protocol and whose ivars are synthesized.
8589820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8599820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8609820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8619820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian    CollectProtocolSynthesizedIvars(PD, Ivars);
8629820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian  }
8639820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
8649820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
8658e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianunsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
8668e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
86717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
86817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = PD->prop_end(); I != E; ++I)
8698e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8708e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8718e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8728e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into nested protocols.
8738e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
8748e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       E = PD->protocol_end(); P != E; ++P)
8758e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(*P);
8768e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8778e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8788e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpunsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
8808e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  unsigned count = 0;
88117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
88217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis       E = OI->prop_end(); I != E; ++I) {
8838e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if ((*I)->getPropertyIvarDecl())
8848e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian      ++count;
8858e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8868e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // Also look into interface's protocol list for properties declared
8878e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  // in the protocol and whose ivars are synthesized.
8888e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
8898e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian       PE = OI->protocol_end(); P != PE; ++P) {
8908e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    ObjCProtocolDecl *PD = (*P);
8918e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    count += CountProtocolSynthesizedIvars(PD);
8928e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  }
8938e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
8948e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
8958e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
8968a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
8978a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
8988a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
8998a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
9008a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
9018a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
9028a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
9038a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
9048a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
9058a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
9068a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
9078a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
9088a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
9098a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
9108a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
9118a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
9128a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
9138a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
9148a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
9158a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
9168a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
9178a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
9188a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
9198a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
9208a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
9218a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
9228a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
9238a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
9248a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
9258a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
926b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \brief Allocate an uninitialized DeclaratorInfo.
927b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
928b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// The caller should initialize the memory held by DeclaratorInfo using
929b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
930b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
931b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
932b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
933b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
934b17166c8077cd900cca83a895c43b30ea6660598Argyrios KyrtzidisDeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
935b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
936b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  DeclaratorInfo *DInfo =
937b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis    (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
938b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  new (DInfo) DeclaratorInfo(T);
939b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis  return DInfo;
940b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
941b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
942b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// getInterfaceLayoutImpl - Get or compute information about the
943b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// layout of the given interface.
944b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar///
945b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// \param Impl - If given, also include the layout of the interface's
946b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar/// implementation. This may differ by including synthesized ivars.
94744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patelconst ASTRecordLayout &
948b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
949b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar                          const ObjCImplementationDecl *Impl) {
950532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar  assert(!D->isForwardDecl() && "Invalid interface decl!");
951532d4daa038d972240138e2fd6e1122517340833Daniel Dunbar
95244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  // Look up this layout, if already laid out, return what we have.
9531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ObjCContainerDecl *Key =
954d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
955d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar  if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
956d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    return *Entry;
95744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
958453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  // Add in synthesized ivar count if laying out an implementation.
959453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  if (Impl) {
96029445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    unsigned FieldCount = D->ivar_size();
9618e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    unsigned SynthCount = CountSynthesizedIvars(D);
9628e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    FieldCount += SynthCount;
963d8fd6ff0ea47ba63f836d0f4e6a1bee49863f64aDaniel Dunbar    // If there aren't any sythesized ivars then reuse the interface
964453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entry. Note we can't cache this because we simply free all
965453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // entries later; however we shouldn't look up implementations
966453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar    // frequently.
9678e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian    if (SynthCount == 0)
968453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar      return getObjCLayout(D, 0);
969453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar  }
970453addbaea98f9678e2f9858057722a028f1ae3cDaniel Dunbar
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
97229445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
97329445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson  ObjCLayouts[Key] = NewEntry;
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
97544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  return *NewEntry;
97644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel}
97744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel
978b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
979b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
980b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
981b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
982b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
983b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
984b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel DunbarASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
985b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
986b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
987b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
98888a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
989464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
990464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
99198be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
9924b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  D = D->getDefinition(*this);
9934b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  assert(D && "Cannot get layout of forward declarations!");
9944bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
995464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
996ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // Note that we can't save a reference to the entry because this function
997ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  // is recursive.
998ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  const ASTRecordLayout *Entry = ASTRecordLayouts[D];
999464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
10004bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const ASTRecordLayout *NewEntry =
100229445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson    ASTRecordLayoutBuilder::ComputeLayout(*this, D);
1003ab22c43320ae813e4f6214f48968216b7cb09b90Eli Friedman  ASTRecordLayouts[D] = NewEntry;
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10055d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
1006464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
1007464175bba1318bef7905122e9fda20cff926df78Chris Lattner
1008a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1009a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1010a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1011a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
10120953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
10130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  unsigned Fast = Quals.getFastQualifiers();
10140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.removeFastQualifiers();
10150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
10170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
10180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQuals::Profile(ID, TypeNode, Quals);
10190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  void *InsertPos = 0;
10200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
10210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert(EQ->getQualifiers() == Quals);
10220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType T = QualType(EQ, Fast);
10230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return T;
10240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
10250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQuals *New = new (*this, 8) ExtQuals(*this, TypeNode, Quals);
10270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  ExtQualNodes.InsertNode(New, InsertPos);
10280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType T = QualType(New, Fast);
10290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return T;
10300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
10310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10320953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualType ASTContext::getVolatileType(QualType T) {
10330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanT = getCanonicalType(T);
10340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (CanT.isVolatileQualified()) return T;
10350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addVolatile();
10390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
10400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
10410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
10420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1043f11284ac87daa613bc7b30db9f54bd716d123222Fariborz JahanianQualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
1044f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1045f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1046f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1047b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
10480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
10490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
10500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
10540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
10550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
10560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
10570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1060ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1061ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1062b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
10630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                       Qualifiers::GC GCAttr) {
1064d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1065b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1066d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10684027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  if (T->isPointerType()) {
10696217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
107058f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
10714027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
10724027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
10734027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
10744027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
10751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
10770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
10780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
10790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
10820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
10830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
10840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
10850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1088d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1089a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
10902455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getNoReturnType(QualType T) {
10910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType ResultType;
10922455636163fdd18581d7fdae816433f886d88213Mike Stump  if (T->isPointerType()) {
10936217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<PointerType>()->getPointeeType();
10940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ResultType = getNoReturnType(Pointee);
10956dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getPointerType(ResultType);
10960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  } else if (T->isBlockPointerType()) {
10976217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
10980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ResultType = getNoReturnType(Pointee);
10996dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    ResultType = getBlockPointerType(ResultType);
11000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  } else {
11010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert (T->isFunctionType()
11020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall            && "can't noreturn qualify non-pointer to function or block type");
11031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (const FunctionNoProtoType *F = T->getAs<FunctionNoProtoType>()) {
11050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      ResultType = getFunctionNoProtoType(F->getResultType(), true);
11060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    } else {
11070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      const FunctionProtoType *F = T->getAs<FunctionProtoType>();
11080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      ResultType
11090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        = getFunctionType(F->getResultType(), F->arg_type_begin(),
11100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                          F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
11110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                          F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
11120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                          F->getNumExceptions(), F->exception_begin(), true);
11130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
11142455636163fdd18581d7fdae816433f886d88213Mike Stump  }
11150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
11160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(ResultType, T.getQualifiers());
11172455636163fdd18581d7fdae816433f886d88213Mike Stump}
11182455636163fdd18581d7fdae816433f886d88213Mike Stump
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
11215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
11225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
11295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1135f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1139f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1141f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ComplexType *New = new (*this,8) ComplexType(T, Canonical);
11425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
11445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1147f98aba35e6c3da5aae61843fc01334939e4e12ecEli FriedmanQualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1148f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1149f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman     SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1150f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  FixedWidthIntType *&Entry = Map[Width];
1151f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (!Entry)
1152f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    Entry = new FixedWidthIntType(Width, Signed);
1153f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  return QualType(Entry, 0);
1154f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman}
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
11575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
1172f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
11731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1176f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1178f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  PointerType *New = new (*this,8) PointerType(T, Canonical);
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
11805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
11855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
11865618bd4a52c45fbbb605e3ba885663b2164db8a3Steve NaroffQualType ASTContext::getBlockPointerType(QualType T) {
1187296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1188296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
11895618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
11905618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
11915618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
11921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11935618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
11945618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
11955618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
11965618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
11971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
11995618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
12005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
12015618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (!T->isCanonical()) {
12025618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
12055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
12065618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1207f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
1209f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
12105618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
12115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
12125618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
12135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
12145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
12157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
12167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
12177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getLValueReferenceType(QualType T) {
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceType::Profile(ID, T);
12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
12247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
12257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
12277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
12327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getLValueReferenceType(getCanonicalType(T));
12337c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
12357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
12367c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
12377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
12397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
12417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
12427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
12437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
12447c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
12457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
12477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
12487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian RedlQualType ASTContext::getRValueReferenceType(QualType T) {
12497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
12507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
12517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
12527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  ReferenceType::Profile(ID, T);
12537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
12557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
12567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
12577c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
12587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12597c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
12607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
12617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
12627c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (!T->isCanonical()) {
12637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    Canonical = getRValueReferenceType(getCanonicalType(T));
12647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
12667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
12677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1268f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12717c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
12725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
12737c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1277f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1278f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
12791eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
1280f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1281f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1282f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1283f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1284f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1285f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1286f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1287f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1288f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1289f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1290f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1291f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1292f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
1293f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (!T->isCanonical()) {
1294f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1295f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1296f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1297f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1298f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1299f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1300f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
1301f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
1302f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1303f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1304f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1305f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1306f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1308fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
13091eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
131038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1311c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
1312c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
1313587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman  assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1314587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1315587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
131638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
131738aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
131838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
131938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
13220be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
13267192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
13275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
13281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!EltTy->isCanonical()) {
13331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
1334c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
13377192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1338f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1341566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  ConstantArrayType *New =
1342f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
13437192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13487e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithExprType - Return a reference to the type for
13497e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13507e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13517e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithExprType(QualType EltTy,
13527e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         const llvm::APInt &ArySizeIn,
13537e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         Expr *ArySizeExpr,
13547e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         ArrayType::ArraySizeModifier ASM,
13557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         unsigned EltTypeQuals,
13567e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                         SourceRange Brackets) {
13577e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13587e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13597e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13617e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
13637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
13647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
13657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Since we don't unique expressions, it isn't possible to unique VLA's
13667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // that have an expression provided for their size.
13677e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithExprType *New =
13687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
13697e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ArySize, ArySizeExpr,
13707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          ASM, EltTypeQuals, Brackets);
13717e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
13727e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
13737e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
13747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13757e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// getConstantArrayWithoutExprType - Return a reference to the type for
13767e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor/// an array of the specified element type.
13777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType
13787e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorASTContext::getConstantArrayWithoutExprType(QualType EltTy,
13797e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            const llvm::APInt &ArySizeIn,
13807e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArrayType::ArraySizeModifier ASM,
13817e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            unsigned EltTypeQuals) {
13827e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Convert the array size into a canonical width matching the pointer
13837e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // size for the target.
13847e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  llvm::APInt ArySize(ArySizeIn);
13857e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
13867e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
13877e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  // Compute the canonical ConstantArrayType.
13887e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
13897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                            ArySize, ASM, EltTypeQuals);
13907e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  ConstantArrayWithoutExprType *New =
13917e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
13927e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                             ArySize, ASM, EltTypeQuals);
13937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  Types.push_back(New);
13947e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return QualType(New, 0);
13957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor}
13967e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor
1397bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1398bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
13997e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
14007e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1401c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
14027e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          unsigned EltTypeQuals,
14037e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          SourceRange Brackets) {
1404c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1405c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
1406c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1407566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek  VariableArrayType *New =
14087e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    new(*this,8)VariableArrayType(EltTy, QualType(),
14097e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  NumElts, ASM, EltTypeQuals, Brackets);
1410c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1411c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1412c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1413c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1414c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1415c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1416898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1417898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
141804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
14197e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getDependentSizedArrayType(QualType EltTy,
14207e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                Expr *NumElts,
1421898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
14227e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                unsigned EltTypeQuals,
14237e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                SourceRange Brackets) {
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1425898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1426898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
142704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  llvm::FoldingSetNodeID ID;
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
142904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                   EltTypeQuals, NumElts);
1430898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
143104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  void *InsertPos = 0;
143204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *Canon
143304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
143404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  DependentSizedArrayType *New;
143504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  if (Canon) {
143604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // We already have a canonical version of this array type; use it as
143704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    // the canonical type for a newly-built type.
14381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    New = new (*this,8) DependentSizedArrayType(*this, EltTy,
143904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                QualType(Canon, 0),
144004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                NumElts, ASM, EltTypeQuals,
144104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                Brackets);
144204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  } else {
144304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    QualType CanonEltTy = getCanonicalType(EltTy);
144404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    if (CanonEltTy == EltTy) {
144504d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      New = new (*this,8) DependentSizedArrayType(*this, EltTy, QualType(),
144604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  NumElts, ASM, EltTypeQuals,
144704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  Brackets);
144804d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      DependentSizedArrayTypes.InsertNode(New, InsertPos);
144904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    } else {
145004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
145104d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  ASM, EltTypeQuals,
145204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  SourceRange());
145304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor      New = new (*this,8) DependentSizedArrayType(*this, EltTy, Canon,
145404d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor                                                  NumElts, ASM, EltTypeQuals,
14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                  Brackets);
145604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor    }
145704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor  }
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1459898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  Types.push_back(New);
1460898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  return QualType(New, 0);
1461898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor}
1462898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
1463c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
1464c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
1465c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
1466c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
14670be2ef2321b1283ead38ebeb83b451335d90e0feChris Lattner  IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
1468c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1469c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (IncompleteArrayType *ATP =
1471c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1472c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
1473c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1474c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
1475c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
1476c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
1477c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1478c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (!EltTy->isCanonical()) {
1479f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
14802bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
1481c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1482c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
1483c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
1484c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1485f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
14862bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1487c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
14887e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  IncompleteArrayType *New
14897e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    = new (*this,8) IncompleteArrayType(EltTy, Canonical,
14907e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                        ASM, EltTypeQuals);
1491c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1492c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
1493c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1494c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1495fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1496fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
149773322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
149873322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
149973322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
15011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1502f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
150373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
15041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  VectorType::Profile(ID, vecType, NumElts, Type::Vector);
15085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
15105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
15115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
15135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
15145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
15155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!vecType->isCanonical()) {
1516f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getVectorType(getCanonicalType(vecType), NumElts);
15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1520f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1522f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1528213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
152973322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
1530213541a68a3e137d11d2cefb612c6cdb410d7e8eNate BegemanQualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
153173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
15321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1533f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1534213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
153673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
153773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
153973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
154073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
154173322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
154273322924127c873c13101b705dd823f5539ffa5fSteve Naroff
154373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
154473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
154573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
154673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (!vecType->isCanonical()) {
1547213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
154973322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
155073322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1551f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
155273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
1553f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
155473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
155573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
155673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
155773322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
155873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
15591eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
15609cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    Expr *SizeExpr,
15619cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor                                                    SourceLocation AttrLoc) {
15622ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
15631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
15642ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15662ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
15672ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
15682ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
15692ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
15702ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
15712ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
15722ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
15732ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
15742ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                    QualType(Canon, 0),
15752ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                    SizeExpr, AttrLoc);
15762ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
15772ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
15782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
15791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                      QualType(), SizeExpr,
15812ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      AttrLoc);
15822ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
15832ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
15842ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
15852ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
15862ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      New = new (*this,8) DependentSizedExtVectorType(*this, vecType, Canon,
15872ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SizeExpr, AttrLoc);
15882ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
15892ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
15901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15919cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
15929cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
15939cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
15949cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
159572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
15965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
15972455636163fdd18581d7fdae816433f886d88213Mike StumpQualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
16012455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
16021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
160572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
16071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!ResultTy->isCanonical()) {
16102455636163fdd18581d7fdae816433f886d88213Mike Stump    Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
161372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
161472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1615f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16182455636163fdd18581d7fdae816433f886d88213Mike Stump  FunctionNoProtoType *New
16192455636163fdd18581d7fdae816433f886d88213Mike Stump    = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn);
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
162172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
16265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
162761710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerQualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
1628971c4fae6092976338b755af1d47dac07c8f16e3Argyrios Kyrtzidis                                     unsigned NumArgs, bool isVariadic,
1629465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     unsigned TypeQuals, bool hasExceptionSpec,
1630465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                     bool hasAnyExceptionSpec, unsigned NumExs,
16312455636163fdd18581d7fdae816433f886d88213Mike Stump                                     const QualType *ExArray, bool NoReturn) {
163283913e36c847052966d9ff60d760ea7231ed8b6bAnders Carlsson  if (LangOpts.CPlusPlus) {
163383913e36c847052966d9ff60d760ea7231ed8b6bAnders Carlsson    for (unsigned i = 0; i != NumArgs; ++i)
16340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      assert(!ArgArray[i].hasQualifiers() &&
16350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall             "C++ arguments can't have toplevel qualifiers!");
163683913e36c847052966d9ff60d760ea7231ed8b6bAnders Carlsson  }
163783913e36c847052966d9ff60d760ea7231ed8b6bAnders Carlsson
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
16395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
164172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1642465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                             TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
16432455636163fdd18581d7fdae816433f886d88213Mike Stump                             NumExs, ExArray, NoReturn);
16445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
164772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
16485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1649465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1650465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
16515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCanonical = ResultTy->isCanonical();
1652465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  if (hasExceptionSpec)
1653465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    isCanonical = false;
16545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
16555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!ArgArray[i]->isCanonical())
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
16575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
1659465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
16605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isCanonical) {
16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
16635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
1665f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner      CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
1666465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1667f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
1668beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
166947259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                isVariadic, TypeQuals, false,
167047259d9ca7840dd66f06f5f11da7768b23d1e0fdDouglas Gregor                                false, 0, 0, NoReturn);
1671465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
16725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
167372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
167472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1675f6e764fe722440eaed18dad9eeff3d7e89a4d7afChris Lattner    assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1677465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
167872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
1679465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
1680465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // end of them.
16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FunctionProtoType *FTP =
1682465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1683465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumArgs*sizeof(QualType) +
1684465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                                 NumExs*sizeof(QualType), 8);
168572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
1686465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl                              TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
16872455636163fdd18581d7fdae816433f886d88213Mike Stump                              ExArray, NumExs, Canonical, NoReturn);
16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
168972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
16905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
16915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16932ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
16942ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
16954b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed KremenekQualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
16961e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
16972ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16991e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
17002ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
1701fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  else if (isa<TemplateTypeParmDecl>(Decl)) {
1702fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    assert(false && "Template type parameter types are always available.");
17039fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else if (ObjCInterfaceDecl *ObjCInterface
17049fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump               = dyn_cast<ObjCInterfaceDecl>(Decl))
17052ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getObjCInterfaceType(ObjCInterface);
170649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
1707c1efaecf0373f1a55c5ef4c234357cf726fc0600Douglas Gregor  if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
1708566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1709566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1710f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1711f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) RecordType(Record);
17129fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1713566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek    if (PrevDecl)
1714566c2ba743065ec090f5154d5c30cf593aa12a6eTed Kremenek      Decl->TypeForDecl = PrevDecl->TypeForDecl;
1715f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff    else
1716f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff      Decl->TypeForDecl = new (*this,8) EnumType(Enum);
17179fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
17182ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    assert(false && "TypeDecl without a type?");
171949aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
17204b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek  if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
172149aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
17222ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
17232ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
17245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
17255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
17265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) {
17275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
17281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1729f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
173072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
17335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1735fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
17361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
173776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
17381eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
173976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
1740fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor                                             IdentifierInfo *Name) {
1741fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
174276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
1743fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
17441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
1745fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1746fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1747fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
1748fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
17491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
175076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  if (Name) {
175176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
175276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
175376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                                   Name, Canon);
175476e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
175576e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
1756fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1757fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
1758fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1759fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
1760fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
1761fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
1762fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
17631eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
17647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
17657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
17667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
17677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          QualType Canon) {
1768b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  if (!Canon.isNull())
1769b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    Canon = getCanonicalType(Canon);
1770b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  else {
1771b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    // Build the canonical template specialization type.
17721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateName CanonTemplate = getCanonicalTemplateName(Template);
17731275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::SmallVector<TemplateArgument, 4> CanonArgs;
17741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    CanonArgs.reserve(NumArgs);
17751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
17761275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
17771275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
17781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // Determine whether this canonical template specialization type already
17791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    // exists.
17801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    llvm::FoldingSetNodeID ID;
17811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TemplateSpecializationType::Profile(ID, CanonTemplate,
1782828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                        CanonArgs.data(), NumArgs, *this);
17831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
17841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    void *InsertPos = 0;
17851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    TemplateSpecializationType *Spec
17861275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
17871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17881275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    if (!Spec) {
17891275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // Allocate a new canonical template specialization type.
17901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      void *Mem = Allocate((sizeof(TemplateSpecializationType) +
17911275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                            sizeof(TemplateArgument) * NumArgs),
17921275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                           8);
17931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
17941275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                                  CanonArgs.data(), NumArgs,
1795b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor                                                  Canon);
17961275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Types.push_back(Spec);
17971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
17981275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
17991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1800b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor    if (Canon.isNull())
1801b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor      Canon = QualType(Spec, 0);
18021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(Canon->isDependentType() &&
18031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           "Non-dependent template-id type must have a canonical type");
1804b88e888404ad0a2bdd9bfae457e8530bb38a87c5Douglas Gregor  }
1805fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
18061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
18071275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
18081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
18091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void *Mem = Allocate((sizeof(TemplateSpecializationType) +
181040808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                        sizeof(TemplateArgument) * NumArgs),
181140808ce6ac04b102c3b56244a635d6b98eed6d97Douglas Gregor                       8);
18121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
1814828e226ab7ed08b3eb766549e9d3306432137460Douglas Gregor                                           Canon);
18151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
181655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
18171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
181855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
181955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
18201eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
1821ab452ba8323d1985e08bade2bced588cddf2cc28Douglas GregorASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
1822e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor                                 QualType NamedType) {
1823e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
1824ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor  QualifiedNameType::Profile(ID, NNS, NamedType);
1825e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
1826e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
18271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  QualifiedNameType *T
1828e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1829e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
1830e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
1831e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
18321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  T = new (*this) QualifiedNameType(NNS, NamedType,
1833ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor                                    getCanonicalType(NamedType));
1834e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
1835e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  QualifiedNameTypes.InsertNode(T, InsertPos);
1836e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
1837e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
1838e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
18391eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1840d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     const IdentifierInfo *Name,
1841d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                     QualType Canon) {
1842d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1843d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1844d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
1845d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1846d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    if (CanonNNS != NNS)
1847d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor      Canon = getTypenameType(CanonNNS, Name);
1848d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
1849d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1850d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
1851d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameType::Profile(ID, NNS, Name);
1852d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1853d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
18541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TypenameType *T
1855d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1856d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
1857d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
1858d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
1859d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  T = new (*this) TypenameType(NNS, Name, Canon);
1860d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
1861d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
18621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
1863d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
1864d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
18651eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
18661eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpASTContext::getTypenameType(NestedNameSpecifier *NNS,
18671734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            const TemplateSpecializationType *TemplateId,
18681734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor                            QualType Canon) {
18691734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
18701734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18711734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (Canon.isNull()) {
18721734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
18731734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
18741734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
18751734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      const TemplateSpecializationType *CanonTemplateId
1876183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = CanonType->getAs<TemplateSpecializationType>();
18771734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      assert(CanonTemplateId &&
18781734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor             "Canonical type must also be a template specialization type");
18791734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor      Canon = getTypenameType(CanonNNS, CanonTemplateId);
18801734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    }
18811734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
18821734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18831734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  llvm::FoldingSetNodeID ID;
18841734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameType::Profile(ID, NNS, TemplateId);
18851734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18861734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  void *InsertPos = 0;
18871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TypenameType *T
18881734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
18891734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  if (T)
18901734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    return QualType(T, 0);
18911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18921734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  T = new (*this) TypenameType(NNS, TemplateId, Canon);
18931734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
18941734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  TypenameTypes.InsertNode(T, InsertPos);
18951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
18961734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
18971734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
18987da2431c23ef1ee8acb114e39692246e1801afc2John McCallQualType
18997da2431c23ef1ee8acb114e39692246e1801afc2John McCallASTContext::getElaboratedType(QualType UnderlyingType,
19007da2431c23ef1ee8acb114e39692246e1801afc2John McCall                              ElaboratedType::TagKind Tag) {
19017da2431c23ef1ee8acb114e39692246e1801afc2John McCall  llvm::FoldingSetNodeID ID;
19027da2431c23ef1ee8acb114e39692246e1801afc2John McCall  ElaboratedType::Profile(ID, UnderlyingType, Tag);
19031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19047da2431c23ef1ee8acb114e39692246e1801afc2John McCall  void *InsertPos = 0;
19057da2431c23ef1ee8acb114e39692246e1801afc2John McCall  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
19067da2431c23ef1ee8acb114e39692246e1801afc2John McCall  if (T)
19077da2431c23ef1ee8acb114e39692246e1801afc2John McCall    return QualType(T, 0);
19087da2431c23ef1ee8acb114e39692246e1801afc2John McCall
19097da2431c23ef1ee8acb114e39692246e1801afc2John McCall  QualType Canon = getCanonicalType(UnderlyingType);
19107da2431c23ef1ee8acb114e39692246e1801afc2John McCall
19117da2431c23ef1ee8acb114e39692246e1801afc2John McCall  T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
19127da2431c23ef1ee8acb114e39692246e1801afc2John McCall  Types.push_back(T);
19137da2431c23ef1ee8acb114e39692246e1801afc2John McCall  ElaboratedTypes.InsertNode(T, InsertPos);
19147da2431c23ef1ee8acb114e39692246e1801afc2John McCall  return QualType(T, 0);
19157da2431c23ef1ee8acb114e39692246e1801afc2John McCall}
19167da2431c23ef1ee8acb114e39692246e1801afc2John McCall
191788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
191888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
191988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
192088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
19212e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
192288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
192388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
192488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
192588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
192688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
192888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
192988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
193088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
193188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
193288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
193388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
193488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
193588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1936d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1937d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff/// the given interface decl and the conforming protocol list.
193814108da7f7fc059772711e4ffee1322a27b152a7Steve NaroffQualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
19391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              ObjCProtocolDecl **Protocols,
1940d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff                                              unsigned NumProtocols) {
1941d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // Sort the protocol list alphabetically to canonicalize it.
1942d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (NumProtocols)
1943d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
1944d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1945d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  llvm::FoldingSetNodeID ID;
194614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
1947d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1948d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
1949d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  if (ObjCObjectPointerType *QT =
1950d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1951d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff    return QualType(QT, 0);
1952d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
1953d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  // No Match;
1954d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerType *QType =
195514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
19561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1957d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  Types.push_back(QType);
1958d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1959d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  return QualType(QType, 0);
1960d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
196188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
1962c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// getObjCInterfaceType - Return the unique reference to the type for the
1963c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff/// specified ObjC interface decl. The list of protocols is optional.
1964c15cb2af27514ecc879daba9aa01389c5203685dSteve NaroffQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
1965a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                       ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
19661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (NumProtocols)
1967c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    // Sort the protocol list alphabetically to canonicalize it.
1968c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    SortAndUniqueProtocols(Protocols, NumProtocols);
19691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19704b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
1971c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
19721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19734b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
1974c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (ObjCInterfaceType *QT =
1975c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff      ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
19764b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19784b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  // No Match;
1979c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceType *QType =
19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1981c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                    Protocols, NumProtocols);
19824b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  Types.push_back(QType);
1983c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  ObjCInterfaceTypes.InsertNode(QType, InsertPos);
19844b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  return QualType(QType, 0);
19854b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian}
19864b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
198772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
198872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
19899752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
19901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
19919752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
199272564e73277e29f6db3305d1f27ba408abb7ed88Douglas GregorQualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
1993dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
1994b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
1995b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
1996b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
19971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1998b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
1999b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2000b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2001b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2002b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2003b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
20041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      toe = new (*this, 8) TypeOfExprType(tofExpr,
2005b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
2006b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2007b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
2008b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
2009b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr);
2010b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2011b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2012b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2013b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2014dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
2015dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
2016dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
20179752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
20189752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2019d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2020d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
20219752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
20229752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
20239752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
20241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
20259752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
2026d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
2027f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
2028f83820bd7a0dc4c253071b31c443a316a49ce5aaSteve Naroff  TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
20299752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
20309752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2031d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2032d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
203360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
203460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
203560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlssonstatic QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
2036a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2037a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
20381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
203960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
204060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
204160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
204260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
204360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
204460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
204560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
204660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
204760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
204860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
204960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
205060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
205160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
205260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
205360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
20541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
205560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
205860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
205960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (e->isLvalue(Context) == Expr::LV_Valid)
206060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
20611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
206260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
206360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
206460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2065395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2066395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2067395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
20681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2069395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
2070395b475a4474f1c7574d927ad142ca0c7997cbcaAnders CarlssonQualType ASTContext::getDecltypeType(Expr *e) {
2071dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
20729d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  if (e->isTypeDependent()) {
20739d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
20749d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
20751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20769d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
20779d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
20789d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
20799d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
20809d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
20819d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
20829d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = new (*this, 8) DecltypeType(e, DependentTy,
20839d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
20849d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
20859d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
20869d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
20879d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      Canon = new (*this, 8) DependentDecltypeType(*this, e);
20889d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
20899d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
20909d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
20919d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2092dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
20931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
2094dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2095395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2096395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2097395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2098395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
2101e607ed068334bacb8d7b093996b4671c6ca79e25Mike StumpQualType ASTContext::getTagDeclType(const TagDecl *Decl) {
2102d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2103e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2104e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2105e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
21065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const {
2112b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
21135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
211564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
211664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
211764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
211864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
211964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
212064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
212164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
212264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
212364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
212464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
212564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
212664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
212764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
212864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
21298b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
21308b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
21318b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2132b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
21338b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
21348b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2135e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2136e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2137e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2138e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
213977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
214077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
214177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
214277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
214377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
214477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
214550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas GregorCanQualType ASTContext::getCanonicalType(QualType T) {
21460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
21470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ptr = Quals.strip(T);
21480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType CanType = Ptr->getCanonicalTypeInternal();
21490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
21500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // The canonical internal type will be the canonical type *except*
21510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // that we push type qualifiers down through array types.
21521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If there are no new qualifiers to push down, stop here.
21540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!Quals.hasQualifiers())
215550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(CanType);
2156c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
21570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the type qualifiers are on an array type, get the canonical
21580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type of the array with the qualifiers applied to the element
21590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // type.
2160c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  ArrayType *AT = dyn_cast<ArrayType>(CanType);
2161c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (!AT)
21620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
21631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2164c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2165c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
21660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
2167c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  NewEltTy = getCanonicalType(NewEltTy);
21681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2169c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
217050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
217150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getConstantArrayType(NewEltTy, CAT->getSize(),
217250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                  CAT->getSizeModifier(),
21730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                  CAT->getIndexTypeCVRQualifiers()));
2174c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
217550d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
217650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
21770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                    IAT->getIndexTypeCVRQualifiers()));
21781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2179898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
218050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor    return CanQualType::CreateUnsafe(
218150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor             getDependentSizedArrayType(NewEltTy,
2182bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                        DSAT->getSizeExpr() ?
2183bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                          DSAT->getSizeExpr()->Retain() : 0,
218450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getSizeModifier(),
21850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                        DSAT->getIndexTypeCVRQualifiers(),
218650d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                        DSAT->getBracketsRange()));
2187898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
2188c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  VariableArrayType *VAT = cast<VariableArrayType>(AT);
218950d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
2190bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                        VAT->getSizeExpr() ?
2191bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr()->Retain() : 0,
219250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                        VAT->getSizeModifier(),
21930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
219450d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor                                                     VAT->getBracketsRange()));
2195c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner}
2196c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
219725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas GregorTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
219825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // If this template name refers to a template, the canonical
219925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  // template name merely stores the template itself.
220025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl())
220197fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
220225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
22031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If this template name refers to a set of overloaded function templates,
2204d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  /// the canonical template name merely stores the set of function templates.
22056ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor  if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
22066ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    OverloadedFunctionDecl *CanonOvl = 0;
22076ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
22086ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                                                FEnd = Ovl->function_end();
22096ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor         F != FEnd; ++F) {
22106ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      Decl *Canon = F->get()->getCanonicalDecl();
22116ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      if (CanonOvl || Canon != F->get()) {
22126ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor        if (!CanonOvl)
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          CanonOvl = OverloadedFunctionDecl::Create(*this,
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                    Ovl->getDeclContext(),
22156ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                                                    Ovl->getDeclName());
22161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22176ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor        CanonOvl->addOverload(
22186ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor                    AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
22196ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor      }
22206ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    }
22211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22226ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor    return TemplateName(CanonOvl? CanonOvl : Ovl);
22236ebd15e81a4d44ac51c24bffe2705586d5edffeeDouglas Gregor  }
22241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
222525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
222625a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
222725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
222825a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
222925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
22301eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
22311275ae098acda31fe0e434510c729fcfed0458a1Douglas GregorASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
22321275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
22331275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
22341275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
22351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22361275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
22371275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Build canonical expression?
22381275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
22391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
22411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
22421275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              Arg.getAsDecl()->getCanonicalDecl());
22431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
22451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
22461275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              *Arg.getAsIntegral(),
22471275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
22481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
22501275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return TemplateArgument(SourceLocation(),
22511275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getAsType()));
22521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22531275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
22541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      // FIXME: Allocate in ASTContext
22551275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
22561275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
22571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
22581275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
22591275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
22601275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
22611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22621275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      TemplateArgument Result;
22631275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
22641275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Result;
22651275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
22661275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
22671275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
22681275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
22691275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
22701275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
22711275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
22721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
2273d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
2274d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
22751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
2276d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
2277d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2278d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
2279d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
2280d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
22811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
2282d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2283d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
2284d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2285d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
2286d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
2287d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
2288d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2289d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2290d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
2291d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
2292d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
22931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this, 0,
22941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2295d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       T.getTypePtr());
2296d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2297d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2298d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
2299d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
2300d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
2301d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2302d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2303d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
2304d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
2305d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2306d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2307c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
2308c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattnerconst ArrayType *ASTContext::getAsArrayType(QualType T) {
2309c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
23100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!T.hasQualifiers()) {
2311c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
2312c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2313c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
2314c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
23151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
2317c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType CType = T->getCanonicalTypeInternal();
23180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (!isa<ArrayType>(CType))
2319c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
23201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
2322c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
2323c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
23241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2325c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
2326c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
232750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
23280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
23290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
23300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *Ty = Qs.strip(T.getDesugaredType());
23311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2332c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
2333c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
23340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (ATy == 0 || Qs.empty())
2335c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
23361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2337c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
2338c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
2339c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the canonical version of the element with the extra qualifiers on it.
2340c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // This can recursively sink qualifiers through multiple levels of arrays.
23410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
23421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2343c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2344c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2345c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
23460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
2347c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2348c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2349c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
23500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
2351898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
23521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
2353898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
2354898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
23551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
2356bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                                DSAT->getSizeExpr() ?
2357bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              DSAT->getSizeExpr()->Retain() : 0,
2358898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
23590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
23607e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
23611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2362c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
23637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
2364bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman                                              VAT->getSizeExpr() ?
23650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getSizeExpr()->Retain() : 0,
2366c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
23670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
23687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
236977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
237077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
237177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
2372e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
2373e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
2374e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
2375e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
2376e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
2377e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2378e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
2379c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
2380c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
2381c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
2382c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
2383c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2384c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
23851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2386c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
2387e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
2388e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
23890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
2390e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
2391e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
23925e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas GregorQualType ASTContext::getBaseElementType(QualType QT) {
23930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Qs;
23945e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  while (true) {
23950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    const Type *UT = Qs.strip(QT);
23965e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
23975e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor      QT = AT->getElementType();
23986dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump    } else {
23990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return Qs.apply(QT);
24005e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor    }
24015e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor  }
24025e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
24035e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
2404d786f6a6b791b5901fa9fd39a2bbf924afbc1252Daniel DunbarQualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
24056183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  QualType ElemTy = VAT->getElementType();
24061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24076183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
24086183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson    return getBaseElementType(VAT);
24091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24106183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson  return ElemTy;
24116183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
24126183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
24130de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
24141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
24150de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
24160de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
24170de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
24180de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
24190de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
24200de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
24210de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
24220de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
24230de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
24245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
24255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
2426a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
2427183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
24285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
2429a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
2430183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2431183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
2432a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
24335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
24345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
24355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
24365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
24375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
24401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
2441716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
2442716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
24431361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
24441361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
24451361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
24461361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
24471361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
2448716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
2449f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
2450f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
2451f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
2452f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
2453f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
24541361b11066239ea15764a2a844405352d87296b3Chris Lattner
24551361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
24561361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
24571361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
24581361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
24591361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
24601361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
24615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
24625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
24657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
24667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
24671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
2468a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2469a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
2470a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
24711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2472a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
2473fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
2474a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
2475fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
2476fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
24775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2479f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2480f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
2481f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
2482f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedmanunsigned ASTContext::getIntegerRank(Type *T) {
2483f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  assert(T->isCanonical() && "T should be canonicalized");
2484f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (EnumType* ET = dyn_cast<EnumType>(T))
2485f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    T = ET->getDecl()->getIntegerType().getTypePtr();
2486f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2487a34267595534a72703290153a6f7e3da1adcec59Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::WChar))
2488a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
2489a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
2490f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
2491f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2492f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2493f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
2494f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2495f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
2496f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // There are two things which impact the integer rank: the width, and
2497f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // the ordering of builtins.  The builtin ordering is encoded in the
2498f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // bottom three bits; the width is encoded in the bits above that.
24991b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
2500f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth() << 3;
2501f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
2502f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
25037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
25047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
2505f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
25067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
25077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
25087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
25097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
2510f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
25117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
25127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
2513f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
25147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
25157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
2516f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
25177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
25187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
2519f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
25207cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
25217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
2522f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
25232df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
25242df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
25252df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
2526f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
2527f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
2528f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
252904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
253004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
253104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
253204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
253304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
253404e8357f6801e9ff52673e7e899a67bbabf9de93Eli FriedmanQualType ASTContext::isPromotableBitField(Expr *E) {
253504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
253604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
253704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
253804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
253904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
254004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
254104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
254204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
254304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
254404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
254504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
254604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
254704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
254804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
254904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
255004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
255104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
255204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
255304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
255404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
255504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
255604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
255704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
255804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
255904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
2560a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
2561a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2562a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
2563a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2564a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
2565a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
2566a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
2567a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
2568a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
2569a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
2570a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2571a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2572a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
2573a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
25741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
25757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
25761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
25777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
2578f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
2579f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
25807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
25811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2582f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2583f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
25841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
25867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
25871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
25897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
25907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
25917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
25921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
25947cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
25957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
25967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
25977cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
25981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
26007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
26011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
26027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
26037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
26047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
26057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
26067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
26077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
26081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
26107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
26111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
26127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
26135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
261471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
26151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
261671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
261771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
26181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
26191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2620df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek                         &Idents.get("NSConstantString"));
2621f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
26221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
262371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
26240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
2625f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
2626f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
262771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
26280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
262971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
26301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
26311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
263244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
263344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
26341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
263544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
2636a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           FieldTypes[i], /*DInfo=*/0,
26371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
26384afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
263917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
264044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
264144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
264244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    CFConstantStringTypeDecl->completeDefinition(*this);
264371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
26441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
26468467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
2647b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
2648319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
26496217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2650319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
2651319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
2652319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2653319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
26541eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getObjCFastEnumerationStateType() {
2655bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
265644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
265744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
265844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                         &Idents.get("__objcFastEnumerationState"));
26591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2660bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
2661bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
2662de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
2663bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
2664bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
2665bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
2666bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
26671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
266844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
26691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this,
26701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           ObjCFastEnumerationStateTypeDecl,
26711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           SourceLocation(), 0,
2672a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis                                           FieldTypes[i], /*DInfo=*/0,
26731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
26744afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
267517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
267644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
26771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
267844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
2679bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
26801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2681bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2682bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
2683bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
2684319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
26856217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
2686319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2687319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2688319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
2689319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
2690e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
2691e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
26922d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
2693e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
2694bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2695bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
26961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
269785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
269885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
269985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
2700a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
270133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
2702a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) {
270398be4943e8dc4f3905629a7102668960873cf863Chris Lattner  uint64_t sz = getTypeSize(type);
27041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
270533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
270633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (sz > 0 && type->isIntegralType())
270798be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = std::max(sz, getTypeSize(IntTy));
270833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
270933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
271098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = getTypeSize(VoidPtrTy);
271198be4943e8dc4f3905629a7102668960873cf863Chris Lattner  return sz / getTypeSize(CharTy);
271233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
271333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2714a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
271533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
27161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
2717e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                              std::string& S) {
2718c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2719ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
2720a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
272133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
27220d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
272333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
272433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
272533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
272633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
272798be4943e8dc4f3905629a7102668960873cf863Chris Lattner  int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
272833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
272933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
273033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  int ParmOffset = 2 * PtrSize;
273189951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
273289951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
273389951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
273489951a86b594513c2a013532ed45d197413b1087Chris Lattner    int sz = getObjCEncodingTypeSize(PType);
2735a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
273633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
273733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
273833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(ParmOffset);
273933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
274033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(PtrSize);
27411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
274233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
274333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
274489951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
274589951a86b594513c2a013532ed45d197413b1087Chris Lattner       E = Decl->param_end(); PI != E; ++PI) {
274689951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
27471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
27484306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
2749ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2750ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
2751ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
2752bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
2753ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
2754ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
2755ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
2756ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
275733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
27584306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
27590d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
276033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    S += llvm::utostr(ParmOffset);
2761a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ParmOffset += getObjCEncodingTypeSize(PType);
276233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
276333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
276433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
2765c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
276683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
2767c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2768c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
27691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
27701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
27711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
27721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
27731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
277483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
277583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
277683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
277783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
277883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
277983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
278083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
278183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
278283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
278383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
278483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
278583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
278683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
278783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
278883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
278983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
27901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2791c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
2792e6db3b09a79d4affaa5c7becbfb6bba3c08430c1Chris Lattner                                                std::string& S) {
2793c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
2794c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
2795c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
2796c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2797c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
2798c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
27991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
2800c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
2801c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
280217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
2803653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2804c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2805c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2806c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2807c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2808c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2809c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2810c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2811c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
2812c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
2813c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
281461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
2815c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
281617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
2817653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
2818c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
2819c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
2820c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2821c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
2822c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
2823c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
2824c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
2825c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
28261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
2827c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2828c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2829c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2830c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
2831c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
2832c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2833c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
2834090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
2835090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
28361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
2837090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
2838090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
2839c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2840c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
2841c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
2842c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
2843c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
2844c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
2845c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
28461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
2847c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
2848c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2849c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2850c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
2851c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
2852c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
2853c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
2854c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2855090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2856090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
28571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2858c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2859c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
2860077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
2861c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2862c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2863c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2864c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
2865077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
2866c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2867c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2868c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
2869c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2870c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
287139f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
2872c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
2873c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2874c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
2875c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
2876c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
2877a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
28781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
28791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
2880a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2881a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
2882a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
28838e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
2884183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
2885c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian      if (BT->getKind() == BuiltinType::ULong &&
2886c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian          ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2887a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
28881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
2889c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian        if (BT->getKind() == BuiltinType::Long &&
2890c657eba43f0159bd81227fa0812b92a0b03f00d0Fariborz Jahanian            ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
2891a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
2892a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2893a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
2894a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
2895a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
28967d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
2897153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                        const FieldDecl *Field) {
289882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
289982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
290082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
290182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
29021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
29035b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
290482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
290582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
29061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void EncodeBitField(const ASTContext *Context, std::string& S,
2907153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                           const FieldDecl *FD) {
29088b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
29098b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
29108b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  ASTContext *Ctx = const_cast<ASTContext*>(Context);
29119a901bb63990574ff0bcc12ff851d7a71cff8ddbEli Friedman  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
29128b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
29138b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
29148b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
29158b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
291682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
291782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
291882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
2919153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
2920090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
29216ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor                                            bool EncodingProperty) {
2922183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
2923ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
2924ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      return EncodeBitField(this, S, FD);
2925ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    char encoding;
2926ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    switch (BT->getKind()) {
29271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: assert(0 && "Unhandled builtin type kind");
2928ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Void:       encoding = 'v'; break;
2929ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Bool:       encoding = 'B'; break;
2930ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_U:
2931ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UChar:      encoding = 'C'; break;
2932ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UShort:     encoding = 'S'; break;
2933ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt:       encoding = 'I'; break;
29341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::ULong:
29351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        encoding =
29361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
293772696e17f90d399448d360cb43aebe5eb2007d4fFariborz Jahanian        break;
2938ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::UInt128:    encoding = 'T'; break;
2939ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::ULongLong:  encoding = 'Q'; break;
2940ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Char_S:
2941ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::SChar:      encoding = 'c'; break;
2942ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Short:      encoding = 's'; break;
2943ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int:        encoding = 'i'; break;
29441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case BuiltinType::Long:
29451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      encoding =
29461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2947ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      break;
2948ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongLong:   encoding = 'q'; break;
2949ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Int128:     encoding = 't'; break;
2950ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Float:      encoding = 'f'; break;
2951ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::Double:     encoding = 'd'; break;
2952ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    case BuiltinType::LongDouble: encoding = 'd'; break;
295343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
29541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2955ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += encoding;
2956ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2957ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
29581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2959183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
2960c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
29611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2962c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
2963ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
2964ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
29651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
296785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    QualType PointeeTy = PT->getPointeeType();
2968a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
2969a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
2970a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
2971a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
29721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
29738e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
2974a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
2975a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2976a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2977a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
29789fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
2979a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
29806217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
29816217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
2982a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
2983a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
2984a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
2985a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2986a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
2987a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
2988a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
2989a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
2990a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
2991a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      const char * s = S.c_str();
2992a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      int len = S.length();
2993a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2994a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        std::string replace = "rn";
2995a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S.replace(S.end()-2, S.end(), replace);
2996a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
2997a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
299814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (isObjCSelType(PointeeTy)) {
29998baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += ':';
30008baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
3001c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian    }
30021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
300385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
300485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
300585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
3006e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
300785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
300885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
300985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
30106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
30119533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
30129533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
30139533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
30149533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
30159533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
30169533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
30179533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
30189533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
30199533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
30209533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
30219533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
302285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
302385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
3024a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
3025a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
30261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
302743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
3028ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3029ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
30301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3031ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
3032ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
3033ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
3034559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    if (isa<IncompleteArrayType>(AT)) {
3035559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
3036559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
3037559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
30381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3039559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3040559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
3041559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
30421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3043559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3044559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += llvm::utostr(CAT->getSize().getZExtValue());
3045559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      else {
3046559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
3047559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3048559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
3049559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
30501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
3052559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
3053559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
3054559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
3055ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3056ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
30571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3058183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
3059c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
3060ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3061ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
30621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
306482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
3065d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
3066502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
3067502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3068502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
3069502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
3070502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
3071502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
30720d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
30737d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
307417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      for (RecordDecl::field_iterator Field = RDecl->field_begin(),
307517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                   FieldEnd = RDecl->field_end();
307644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor           Field != FieldEnd; ++Field) {
307743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (FD) {
3078d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
307944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor          S += Field->getNameAsString();
3080d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar          S += '"';
3081d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
30821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3083d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        // Special case bit-fields.
308443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian        if (Field->isBitField()) {
30851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
308643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     (*Field));
3087d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        } else {
3088a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          QualType qt = Field->getType();
3089a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          getLegacyIntegralTypeEncoding(qt);
30901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          getObjCEncodingForTypeImpl(qt, S, false, true,
309143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                     FD);
3092d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
30937d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
30946de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
3095d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
3096ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3097ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
30981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3099ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
31008b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
31018b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      EncodeBitField(this, S, FD);
31028b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
31038b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
3104ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3105ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
31061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3107ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
310821a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
3109ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3110ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
31111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
311343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
31140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
311543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
311643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
311743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
311843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
3119f1690858344968358131f8d5690d9ee458883000Chris Lattner    llvm::SmallVector<FieldDecl*, 32> RecFields;
312043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    CollectObjCIvars(OI, RecFields);
3121f1690858344968358131f8d5690d9ee458883000Chris Lattner    for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
312243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      if (RecFields[i]->isBitField())
31231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
312443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   RecFields[i]);
312543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
31261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
312743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                                   FD);
312843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
312943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
3130ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
313143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
31321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3133183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
313414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
313514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
313614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3137ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
31381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3139ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCClassType()) {
314014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
314114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3142ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
31431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3144ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
31451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
314614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
314714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
314814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
314914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
315014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
315114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
315267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
315367ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
315414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
315514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
315614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
315714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
315814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
315914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
316014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
3161ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
31621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3163ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
3164ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
3165ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
3166ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
31671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
3168ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
3169ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
31701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
31711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
3172ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
317314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
317414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
3175ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
3176ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
3177ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD || EncodingProperty) {
3178ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
317967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      S += OPT->getInterfaceDecl()->getNameAsCString();
318067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
318167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
3182ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
3183ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
3184ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
31851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
3186ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
3187ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
3188ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
3189ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
31901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3191ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
319285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
319385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
31941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
3195ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
3196ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
3197ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
3198ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
3199ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
3200ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
3201ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
3202ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
3203ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
3204ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
3205ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
3206ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
3207ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
3208ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
3209ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
3210ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
3211b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
32121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3213b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
3214b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
3215b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3216ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
3217de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
32187e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
32197e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
3220ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
3221319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCSelType = T;
3222319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
3223183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const TypedefType *TT = T->getAs<TypedefType>();
3224319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  if (!TT)
3225319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor    return;
3226319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  TypedefDecl *TD = TT->getDecl();
3227b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3228b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  // typedef struct objc_selector *SEL;
32296217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
3230c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!ptr)
3231c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
3232b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
3233c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian  if (!rec)
3234c55a24095c3488fa6e99b537be64e57a2905477bFariborz Jahanian    return;
3235b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  SelStructType = rec;
3236b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
3237b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
3238ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
3239a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
3240390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
3241390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
3242ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
3243de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
32448baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
32458baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
3246a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
32471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
32482198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
32491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3250a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
32512198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
32522198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
32537532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a qualified
32547532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c std::vector.
32551eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
32567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  bool TemplateKeyword,
32577532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  TemplateDecl *Template) {
32587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
32597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
32607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
32627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  QualifiedTemplateName *QTN =
32637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
32647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (!QTN) {
32657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
32667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
32677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
32687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
32707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
32717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3272d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
3273d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
32741eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
3275d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                                  bool TemplateKeyword,
3276d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor                                            OverloadedFunctionDecl *Template) {
3277d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
3278d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
32791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3280d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
3281d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
3282d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3283d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
3284d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3285d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3286d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
32871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3288d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
3289d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
3290d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
32917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
32927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
32931eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
32947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                                  const IdentifierInfo *Name) {
32951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
32963b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
32977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
32987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
32997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
33007532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33017532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
33027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
33037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
33047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
33067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
33077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33087532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
33097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
33107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
33117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
33127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
33137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
33147532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
33157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
33167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
33177532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
33187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
33197532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
3320b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
3321d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
3322d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
3323d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas GregorQualType ASTContext::getFromTargetType(unsigned Type) const {
3324b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
33251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TargetInfo::NoInt: return QualType();
3326b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
3327b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
3328b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
3329b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
3330b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
3331b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
3332b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
3333b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3334b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
3335b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
3336b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
3337b3ac5434ab936f092b8cc48349cb01db3a1e1c76Daniel Dunbar  return QualType();
3338b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
3339b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3340b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3341b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
3342b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
3343b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
3344fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
3345fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
3346fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
3347f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
3348fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
3349fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
3350fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3351fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian    if (TypedefDecl *TD = TDT->getDecl())
335240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
3353fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
3354fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
33551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
3356fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
3357fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
33584fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
33594fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
33604fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
33610953e767ff7817f97b3ab20896b229891eeff45bJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
33620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers::GC GCAttrs = Qualifiers::GCNone;
33634fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  if (getLangOptions().ObjC1 &&
33644fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian      getLangOptions().getGCMode() != LangOptions::NonGC) {
3365b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner    GCAttrs = Ty.getObjCGCAttr();
33664fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian    // Default behavious under objective-c's gc is for objective-c pointers
33671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // (or pointers to them) be treated as though they were declared
3368a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    // as __strong.
33690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GCAttrs == Qualifiers::GCNone) {
337075212ee91313bc1b6dd826d9b173541bc4016539Fariborz Jahanian      if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
33710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        GCAttrs = Qualifiers::Strong;
3372a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian      else if (Ty->isPointerType())
33736217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
3374a223cca7751637f8ec1a860010c4148757fb4752Fariborz Jahanian    }
3375c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // Non-pointers have none gc'able attribute regardless of the attribute
3376c2112181b96349eb595dc5e8b7073b81ecdec0dbFariborz Jahanian    // set on them.
3377f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff    else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
33780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return Qualifiers::GCNone;
33794fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
3380b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
33814fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
33824fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
33836ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
33846ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
33856ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
3386770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
33871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
33886ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
33896ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
33906ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
33916ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  assert(LHS->isCanonical() && RHS->isCanonical());
33926ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
339361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
33946ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
33956ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
33964084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
33974084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
33984084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
33994084c306635b70f37029dca938444e6013f08684Steve Naroff
34004084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
34014084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
34020fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanianbool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
34030fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian                                                ObjCProtocolDecl *rProto) {
34044084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
340514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
34064084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
34074084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
34084084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
34094084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
34104084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
34114084c306635b70f37029dca938444e6013f08684Steve Naroff}
341214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
34134084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
34144084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
34154084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
34164084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
34174084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
34184084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
34194084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
34204084c306635b70f37029dca938444e6013f08684Steve Naroff}
34214084c306635b70f37029dca938444e6013f08684Steve Naroff
34224084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
34234084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
34244084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
34254084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
34264084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
34271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
34284084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
34294084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
34301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
34314084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
34324084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
34334084c306635b70f37029dca938444e6013f08684Steve Naroff
34344084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
3435183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
34361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34374084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
34381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34394084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
34401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
34414084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
34424084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
34434084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34444084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
34454084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
34464084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
34474084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
34480fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
34494084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
34504084c306635b70f37029dca938444e6013f08684Steve Naroff        }
34514084c306635b70f37029dca938444e6013f08684Steve Naroff      }
34524084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
34534084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
34544084c306635b70f37029dca938444e6013f08684Steve Naroff    }
34551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
34564084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34574084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
34584084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
34594084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
3460de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
3461de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
3462de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
3463de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
34644084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
34654084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
34664084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
34674084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
34684084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
34694084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
34708f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
34718f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
3472de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
34731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
34744084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
34754084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
34764084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
34774084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
34784084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
34794084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
34804084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
34810fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
34824084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
34834084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
34844084c306635b70f37029dca938444e6013f08684Steve Naroff          }
34854084c306635b70f37029dca938444e6013f08684Steve Naroff        }
34864084c306635b70f37029dca938444e6013f08684Steve Naroff      }
34874084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
3488de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
3489de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
34901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3491de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
3492de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
34931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34944084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
34954084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
34964084c306635b70f37029dca938444e6013f08684Steve Naroff
34971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
34984084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
34994084c306635b70f37029dca938444e6013f08684Steve Naroff    if (lhsOPT->qual_empty()) {
35004084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
35014084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
35024084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
35034084c306635b70f37029dca938444e6013f08684Steve Naroff             E = rhsQID->qual_end(); I != E; ++I) {
35044084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
35054084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
35064084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
35070fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (lhsID->ClassImplementsProtocol(*I, true)) {
35084084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
35094084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
35104084c306635b70f37029dca938444e6013f08684Steve Naroff          }
35114084c306635b70f37029dca938444e6013f08684Steve Naroff        }
35124084c306635b70f37029dca938444e6013f08684Steve Naroff        if (!match)
35134084c306635b70f37029dca938444e6013f08684Steve Naroff          return false;
35144084c306635b70f37029dca938444e6013f08684Steve Naroff      }
35154084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
35164084c306635b70f37029dca938444e6013f08684Steve Naroff    }
35171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
35184084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
35194084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
35204084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
35214084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
35224084c306635b70f37029dca938444e6013f08684Steve Naroff
35234084c306635b70f37029dca938444e6013f08684Steve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
35244084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
35254084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
35264084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
35274084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
35284084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
35294084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
35304084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
35314084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
35324084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
35334084c306635b70f37029dca938444e6013f08684Steve Naroff        }
35344084c306635b70f37029dca938444e6013f08684Steve Naroff      }
35354084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
35364084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
35374084c306635b70f37029dca938444e6013f08684Steve Naroff    }
35384084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
35394084c306635b70f37029dca938444e6013f08684Steve Naroff  }
35404084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
35414084c306635b70f37029dca938444e6013f08684Steve Naroff}
35424084c306635b70f37029dca938444e6013f08684Steve Naroff
35434084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
35444084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
35454084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
35464084c306635b70f37029dca938444e6013f08684Steve Naroff///
35474084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
35484084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
35494084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
35504084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
35514084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
35524084c306635b70f37029dca938444e6013f08684Steve Naroff
35534084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
35541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
35551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
35564084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
35574084c306635b70f37029dca938444e6013f08684Steve Naroff
35584084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
35594084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
35604084c306635b70f37029dca938444e6013f08684Steve Naroff  if (LHS && RHS) // We have 2 user-defined types.
35614084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
35621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35634084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
356414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
356514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
35663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
35673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman                                         const ObjCInterfaceType *RHS) {
35686ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
35696ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
35706ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
35716ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
35721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35736ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
35746ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
3575c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
35766ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
35771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35786ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't, then it
35796ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // isn't a superset.
3580c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (RHS->getNumProtocols() == 0)
35816ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;  // FIXME: should return false!
35821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3583c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3584c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff                                        LHSPE = LHS->qual_end();
358591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
358691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
358791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
358891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
358991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
3590c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff    for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
35914084c306635b70f37029dca938444e6013f08684Steve Naroff                                          RHSPE = RHS->qual_end();
35928f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
35938f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
359491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
35958f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
35968f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
359791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
359891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
359991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
360091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
360191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
360291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
360391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
36046ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
36056ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
3606389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3607389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
3608183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3609183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
36101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
361114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
3612389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
361314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
361414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
361514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
3616389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
3617389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
36181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
3619ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
36201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
3621ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
36223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedmanbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
36233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  return !mergeTypes(LHS, RHS).isNull();
36243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
36253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36263d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
3627183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
3628183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
362972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
363072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
36313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
36323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
36333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
36353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
36363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
363761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
363861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
363961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner  if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
364061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
36416dcbc294f9b2c45680dd3227e885712c2814ec0eMike Stump  // FIXME: double check this
36422455636163fdd18581d7fdae816433f886d88213Mike Stump  bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
36432455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != lbase->getNoReturnAttr())
36442455636163fdd18581d7fdae816433f886d88213Mike Stump    allLTypes = false;
36452455636163fdd18581d7fdae816433f886d88213Mike Stump  if (NoReturn != rbase->getNoReturnAttr())
36462455636163fdd18581d7fdae816433f886d88213Mike Stump    allRTypes = false;
36471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
3649465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3650465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
36513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
36523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
36533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
36553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
36563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
36573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
36593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
36603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
36613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36627fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
36637fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
36647fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
36653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
36663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
36673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
36683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
36693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
36703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argtype = mergeTypes(largtype, rargtype);
36713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
36723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
367361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
367461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
367561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
367661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
36773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
36783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
36793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
36803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, types.begin(), types.size(),
36812455636163fdd18581d7fdae816433f886d88213Mike Stump                           lproto->isVariadic(), lproto->getTypeQuals(),
36822455636163fdd18581d7fdae816433f886d88213Mike Stump                           NoReturn);
36833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
36843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
36853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
36863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
36873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
368872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
36893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
3690465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
36913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
36923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
36933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
36943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
36953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
36963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
36973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
36983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
36993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
37003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
37013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
37023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
37033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
37043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
37063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
37073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
37082d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getNumArgs(), proto->isVariadic(),
37092d3c191e1d5545e1724ee6e0550c70eef54beff2Mike Stump                           proto->getTypeQuals(), NoReturn);
37103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
37113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
37133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
37142455636163fdd18581d7fdae816433f886d88213Mike Stump  return getFunctionNoProtoType(retType, NoReturn);
37153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
37163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37173d815e7eb56c25d7ed812eced32e41df43039f9aEli FriedmanQualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
371843d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
371943d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
372043d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
37217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
37227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
37233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // FIXME: C++ shouldn't be going through here!  The rules are different
37243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // enough that they should be handled separately.
37257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
37267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // shouldn't be going through here!
37276217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
3728c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    LHS = RT->getPointeeType();
37296217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
3730c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    RHS = RT->getPointeeType();
37313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
37333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
37343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3735f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
37363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
37373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
37383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
37390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
37400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers LQuals = LHSCan.getQualifiers();
37410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Qualifiers RQuals = RHSCan.getQualifiers();
37420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
37430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
37440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
37450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
37460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace())
37470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
37480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
37490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
37500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
37510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
37520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
37530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
37540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
37550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
37560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
37570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
37580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
37590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
37600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
37610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
37620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
37630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
37640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
37650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
37660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
37673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
37680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
37690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
37700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
37713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
3772852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
3773852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
3774f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
37751adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
37761adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
37771adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
37781adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
37794c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
37804c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
3781a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3782a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
3783a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3784a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
37851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3786213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
3787213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3788213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
37891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37904e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
37914e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
37921adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
37931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
3794183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
37953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
37963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
3797bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
3798183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
37993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
38003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
3801bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
38021adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
38033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
3804ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
38053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
38064a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
38071adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
380872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
380972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
381072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
381172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
381272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
381372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
381472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
381572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
38167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
38177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
381872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
381972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
382072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
382172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
382272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
382372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
382472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
382572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
382672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
382772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
382872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
38291adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
38303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
38313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
38326217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
38336217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
38343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
38353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
383607d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
383761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
383807d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
383961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
38403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
38413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
3842c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
3843c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
3844c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
38456217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
38466217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
3847c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3848c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
3849c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3850c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
3851c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3852c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
3853c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
3854c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
38551adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
38563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
38573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
38583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
38593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
38603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
38613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
38623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
38633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
38643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType ResultType = mergeTypes(LHSElem, RHSElem);
38653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
386661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
386761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
386861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
386961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
38703bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
38713bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
38723bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
38733bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
38743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
38753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
387661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
387761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
387861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
387961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
38803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
38813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
38823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
38833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
38843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
38853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
38863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
38873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
38883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
38893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
38903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
38913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
38923bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
38933bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
38947e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
38957e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
38963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
38971adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
38983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return mergeFunctionTypes(LHS, RHS);
389972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
390072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
39013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
39021adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
39033cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
39043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
390564cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
390664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
390764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
39083cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
39095a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
3910183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
39113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
391261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
391361490e9a965cfee8a78c12c6802138844f04250dCedric Venet  case Type::ObjCInterface: {
39145fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    // Check if the interfaces are assignment compatible.
39155a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
39165a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
3917183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3918183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
39195fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff    if (LHSIface && RHSIface &&
39205fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff        canAssignObjCInterfaces(LHSIface, RHSIface))
39215fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
39225fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
39233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
392461490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
392514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
3926183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3927183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
392814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
392914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
3930bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
393114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
39325a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman  case Type::FixedWidthInt:
39335a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // Distinct fixed-width integers are not compatible.
39345a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    return QualType();
39357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  case Type::TemplateSpecialization:
39367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    assert(false && "Dependent types have no size");
39377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
3938ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
393972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
394072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
3941ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
39427192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
39435426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
3944ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
3945ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
394688054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
3947ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedmanunsigned ASTContext::getIntWidth(QualType T) {
3948ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  if (T == BoolTy)
3949ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return 1;
3950f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3951f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return FWIT->getWidth();
3952f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  }
3953f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
3954ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
3955ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
3956ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
3957ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3958ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert(T->isSignedIntegerType() && "Unexpected type");
3959183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const EnumType* ETy = T->getAs<EnumType>())
3960ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
3961183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType* BTy = T->getAs<BuiltinType>();
3962ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  assert (BTy && "Unexpected signed integer type");
3963ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
3964ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
3965ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
3966ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
3967ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
3968ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
3969ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
3970ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
3971ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
3972ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
3973ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
3974ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
39752df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
39762df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
3977ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
3978ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
3979ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
3980ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
3981ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
3982ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
39832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorExternalASTSource::~ExternalASTSource() { }
39842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
39852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid ExternalASTSource::PrintStats() { }
398686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
398786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
398886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
398986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
399086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
399186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
399286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
399386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// pointer over the consumed characters.  This returns the resultant type.
39941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
399586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
399686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  bool AllowTypeModifiers = true) {
399786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
399886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
399986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
40001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
400186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the modifiers first.
400286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
400386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
400486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
40051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
400686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
400786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
400886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
400986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
401086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
401186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
401286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
401386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
401486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
401586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
401686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
401786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
401886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
401986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
402086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
402186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
402286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
402386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
40241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
402586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
402686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
402786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
402886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
402986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
403086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
403186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
403286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
403386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
403486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
403586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
403686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
403786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
403886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
403986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
404086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
404186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
404286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
404386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
404486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
404586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
404686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
404786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
404886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
404986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
405086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
405186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
405286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
405386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
405486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
405586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
405686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
405786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
405886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
405986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
406086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
406186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
406286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
406386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
406486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
406586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
406686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
406786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
406886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
406986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
407086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
407186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
407286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
407386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
407486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
407586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
407686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
407786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
407886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
407986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
408086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
408186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
408286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
408386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
408486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
408586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
408686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
408786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
408886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
408986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
409086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
409186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
409286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
409386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
409486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
409586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
409686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
409786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
409886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Type->isArrayType()) {
409986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
410086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    } else {
410186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
410286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
410386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
410486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
410586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
410686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
410786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
41081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
41101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
411186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
411286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getVectorType(ElementType, NumElements);
411386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
411486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
41159a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
4116c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
4117c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
4118f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
411986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
412086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
4121fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
41229a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
4123f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
4124782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
4125f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
4126f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
4127f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
4128fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
4129f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
4130fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
4131fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
4132fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
4133782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
41341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
413586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (!AllowTypeModifiers)
413686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return Type;
41371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
413886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Done = false;
413986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
414086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
414186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      default: Done = true; --Str; break;
414286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '*':
414386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getPointerType(Type);
414486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
414586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case '&':
414686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        Type = Context.getLValueReferenceType(Type);
414786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
414886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      // FIXME: There's no way to have a built-in with an rvalue ref arg.
414986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      case 'C':
41500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        Type = Type.withConst();
415186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner        break;
415286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
415386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
41541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
415586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
415686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
415786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
415886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
415986df27bbdbb98c39ec2184695c0561209f91beddChris LattnerQualType ASTContext::GetBuiltinType(unsigned id,
416086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                    GetBuiltinTypeError &Error) {
416186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(id);
41621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
416386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
41641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
416586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
416686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
416786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
416886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
416986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
417086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
417186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
417286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
417386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
417486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
417586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
417686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
41771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
417886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
417986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
418086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
418186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
418286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
418386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
418486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
418586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (ArgTypes.size() == 0 && TypeStr[0] == '.')
418686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return getFunctionNoProtoType(ResType);
418786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
418886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                         TypeStr[0] == '.', 0);
418986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
4190a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4191a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanQualType
4192a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli FriedmanASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4193a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Perform the usual unary conversions. We do this early so that
4194a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // integral promotions to "int" can allow us to exit early, in the
4195a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // lhs == rhs check. Also, for conversion purposes, we ignore any
4196a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // qualifiers.  For example, "const float" and "float" are
4197a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // equivalent.
4198a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isPromotableIntegerType())
4199a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = getPromotedIntegerType(lhs);
4200a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4201a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    lhs = lhs.getUnqualifiedType();
4202a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (rhs->isPromotableIntegerType())
4203a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = getPromotedIntegerType(rhs);
4204a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  else
4205a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    rhs = rhs.getUnqualifiedType();
4206a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4207a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If both types are identical, no conversion is needed.
4208a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs == rhs)
4209a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
42101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4211a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4212a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The caller can deal with this (e.g. pointer + int).
4213a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4214a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs;
42151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // At this point, we have two different arithmetic types.
42171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4218a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Handle complex types first (C99 6.3.1.8p1).
4219a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexType() || rhs->isComplexType()) {
4220a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the complex type.
42211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
4222a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
4223a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4224a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
42251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
4226a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
4227a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4228a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4229a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // This handles complex/complex, complex/float, or float/complex.
42301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // When both operands are complex, the shorter operand is converted to the
42311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // type of the longer, and that is the type of the result. This corresponds
42321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // to what is done when combining two real floating-point operands.
42331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // The fun begins when size promotion occur across type domains.
4234a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // From H&S 6.3.4: When one operand is complex and the other is a real
42351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // floating-point type, the less precise type is converted, within it's
4236a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // real or complex domain, to the precision of the other type. For example,
42371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // when combining a "long double" with a "double _Complex", the
4238a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // "double _Complex" is promoted to "long double _Complex".
4239a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
42401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (result > 0) { // The left side is bigger, convert rhs.
4242a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
42431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    } else if (result < 0) { // The right side is bigger, convert lhs.
4244a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
42451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
4246a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // At this point, lhs and rhs have the same rank/size. Now, make sure the
4247a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // domains match. This is a requirement for our implementation, C99
4248a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // does not require this promotion.
4249a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4250a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4251a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return rhs;
4252a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      } else { // handle "_Complex double, double".
4253a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs;
4254a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      }
4255a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4256a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return lhs; // The domain/size match exactly.
4257a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4258a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Now handle "real" floating types (i.e. float, double, long double).
4259a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4260a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // if we have an integer operand, the result is the real floating type.
4261a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isIntegerType()) {
4262a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the lhs floating point type.
4263a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4264a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4265a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (rhs->isComplexIntegerType()) {
4266a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert rhs to the complex floating point type.
4267a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(lhs);
4268a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4269a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhs->isIntegerType()) {
4270a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the rhs floating point type.
4271a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4272a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
42731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (lhs->isComplexIntegerType()) {
4274a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert lhs to the complex floating point type.
4275a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return getComplexType(rhs);
4276a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4277a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // We have two real floating types, float/complex combos were handled above.
4278a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Convert the smaller operand to the bigger result.
4279a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    int result = getFloatingTypeOrder(lhs, rhs);
4280a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (result > 0) // convert the rhs
4281a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4282a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    assert(result < 0 && "illegal float comparison");
4283a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return rhs;   // convert the lhs
4284a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4285a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4286a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Handle GCC complex int extension.
4287a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4288a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4289a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
4290a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    if (lhsComplexInt && rhsComplexInt) {
42911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
4292a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman                              rhsComplexInt->getElementType()) >= 0)
4293a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman        return lhs; // convert the rhs
4294a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4295a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (lhsComplexInt && rhs->isIntegerType()) {
4296a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the rhs to the lhs complex type.
4297a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return lhs;
4298a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    } else if (rhsComplexInt && lhs->isIntegerType()) {
4299a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      // convert the lhs to the rhs complex type.
4300a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman      return rhs;
4301a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    }
4302a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4303a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // Finally, we have two differing integer types.
4304a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  // The rules for this case are in C99 6.3.1.8
4305a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  int compare = getIntegerTypeOrder(lhs, rhs);
4306a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  bool lhsSigned = lhs->isSignedIntegerType(),
4307a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman       rhsSigned = rhs->isSignedIntegerType();
4308a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  QualType destType;
4309a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (lhsSigned == rhsSigned) {
4310a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // Same signedness; use the higher-ranked type
4311a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = compare >= 0 ? lhs : rhs;
4312a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (compare != (lhsSigned ? 1 : -1)) {
4313a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The unsigned type has greater than or equal rank to the
4314a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // signed type, so use the unsigned type
4315a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? rhs : lhs;
4316a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4317a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The two types are different widths; if we are here, that
4318a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // means the signed type is larger than the unsigned type, so
4319a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // use the signed type.
4320a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = lhsSigned ? lhs : rhs;
4321a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  } else {
4322a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // The signed type is higher-ranked than the unsigned type,
4323a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // but isn't actually any bigger (like unsigned int and long
4324a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // on most 32-bit systems).  Use the unsigned type corresponding
4325a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    // to the signed type.
4326a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4327a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  }
4328a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return destType;
4329a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
4330