ASTContext.cpp revision 3c3b7f90a863af43fa63043d396553ecf205351c
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"
15bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck#include "clang/AST/CharUnits.h"
1649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
17980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
18aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
21ea1471e0e967548c596a71469702f8846dbaf3c0John McCall#include "clang/AST/ExprCXX.h"
222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
237b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
2419cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
2514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Mangle.h"
261b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
27a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
29f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
3085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
316fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
32f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
330c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek#include "llvm/Support/Capacity.h"
34071cc7deffad608165b1ddd5263e8bf181861520Charles Davis#include "CXXABI.h"
352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis#include <map>
3629445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
391827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructors;
401827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
41225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructors;
42225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructorsDeclared;
43ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructors;
44ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructorsDeclared;
45a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperators;
46a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
47ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperators;
48ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared;
494923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructors;
504923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructorsDeclared;
514923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
53aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  HalfRank, FloatRank, DoubleRank, LongDoubleRank
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
6161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
7661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddPointer(NTTP->getType().getAsOpaquePtr());
786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I)
826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ID.AddPointer(NTTP->getExpansionType(I).getAsOpaquePtr());
836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
853e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
863e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
873e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
883e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
893e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
903e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
913e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
923e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
933e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
943e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
953e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
964ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
973e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
983e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
993e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
1003e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  void *InsertPos = 0;
1013e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
1023e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
1033e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
1043e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
1053e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1063e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
1073e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
1085f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> CanonParams;
1093e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
1103e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1113e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
1123e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
1133e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
1143e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
1153e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
116344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
117344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
118344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
1193e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->getIndex(), 0, false,
1203e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
1213e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
1226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
1246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
1256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
1266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 2> ExpandedTypes;
1285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
1296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
1316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
1326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
1336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
136ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
137ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
1386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
1396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
1406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
1416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
1426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
1436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
1446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
1456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
1466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
147ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
148ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
1496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
1506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
1516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
1526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
1536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
1546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
1566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
1583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
1593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
1603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
1613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
1633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
1643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
16561c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
16661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
16761c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       0,
1683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
1693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
1703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
1713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
1723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
1733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
1753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
1763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  assert(Canonical == 0 && "Shouldn't be in the map!");
1773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
1783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
1803e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
1813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
1823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
1833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
1843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
185071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
186ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  if (!LangOpts.CPlusPlus) return 0;
187ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
18820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  switch (T.getCXXABI()) {
189ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_ARM:
190ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall    return CreateARMCXXABI(*this);
191ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_Itanium:
192071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
19320cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  case CXXABI_Microsoft:
19420cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
19520cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
196ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  return 0;
197071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
198071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
199bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorstatic const LangAS::Map *getAddressSpaceMap(const TargetInfo &T,
200207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
201207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
202207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
203207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
204207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
205207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
206207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
207207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      3  // opencl_constant
208207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
209bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &FakeAddrSpaceMap;
210207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
211bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &T.getAddressSpaceMap();
212207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
213207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
214207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
2153e3cd93b2fd9644e970c389e715c13883faf68b6Douglas GregorASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM,
216bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       const TargetInfo *t,
217e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
2181b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
219bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       unsigned size_reserve,
220bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       bool DelayInitialization)
221bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  : FunctionProtoTypes(this_()),
222bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    TemplateSpecializationTypes(this_()),
223bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DependentTemplateSpecializationTypes(this_()),
224bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SubstTemplateTemplateParmPacks(this_()),
225bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    GlobalNestedNameSpecifier(0),
226bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Int128Decl(0), UInt128Decl(0),
227bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0),
228e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0),
229bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    FILEDecl(0),
230bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    jmp_bufDecl(0), sigjmp_bufDecl(0), BlockDescriptorType(0),
231bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BlockDescriptorExtendedType(0), cudaConfigureCallDecl(0),
232bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    NullTypeSourceInfo(QualType()),
233bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SourceMgr(SM), LangOpts(LOpts),
23430c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts),
235bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Idents(idents), Selectors(sels),
236bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BuiltinInfo(builtins),
237bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DeclarationNames(*this),
23830c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    ExternalSource(0), Listener(0),
239bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    LastSDM(0, 0),
240bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    UniqueBlockByRefTypeID(0)
241bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor{
2421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
243e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
244bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
245bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (!DelayInitialization) {
246bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    assert(t && "No target supplied for ASTContext initialization");
247bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    InitBuiltinTypes(*t);
248bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  }
249e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
250e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
2523478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
2533478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
2543478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
2557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
25663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
25763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
25863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
2590054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  // Release all of the memory associated with overridden C++ methods.
2617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
2627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor         OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
2637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor       OM != OMEnd; ++OM)
2647d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    OM->second.Destroy();
2653478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
266dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
26763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
26863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
26963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
27063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
27163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
27263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
27363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
27463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
275dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
276dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
277dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
278dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
279dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
280dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
2816320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
2826320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
2836320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
2846320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
2856320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
2866320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
287ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
2880054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
2890054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
2900054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
2910054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
2942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
2952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
298cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "\n*** AST Context Stats:\n";
299cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "  " << Types.size() << " types total.\n";
3007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
301dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
3021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
303dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
304dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
305dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
306dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
307c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
310dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
313dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
314dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
315dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
316dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
317cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "    " << counts[Idx] << " " << #Name               \
318cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " types\n";                                         \
319dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
320dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
321dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
322dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
324cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "Total bytes = " << TotalBytes << "\n";
325cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
3264923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
327cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
328cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDefaultConstructors
329cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit default constructors created\n";
330cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
331cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyConstructors
332cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy constructors created\n";
333ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt  if (getLangOptions().CPlusPlus)
334cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
335cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveConstructors
336cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move constructors created\n";
337cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
338cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyAssignmentOperators
339cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy assignment operators created\n";
340ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt  if (getLangOptions().CPlusPlus)
341cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
342cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveAssignmentOperators
343cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move assignment operators created\n";
344cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDestructorsDeclared << "/"
345cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDestructors
346cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit destructors created\n";
347cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
3482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
349cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "\n";
3502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
3512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
352cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
35363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
356772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getInt128Decl() const {
357772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!Int128Decl) {
358772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty);
359772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
360772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
361772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
362772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
363772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__int128_t"),
364772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
365772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
366772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
367772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return Int128Decl;
368772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
369772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
370772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getUInt128Decl() const {
371772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!UInt128Decl) {
372772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty);
373772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
374772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
375772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
376772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
377772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__uint128_t"),
378772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
379772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
380772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
381772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return UInt128Decl;
382772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
384e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
3856b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
386e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
3876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
390bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorvoid ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
391bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  assert((!this->Target || this->Target == &Target) &&
392bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor         "Incorrect target reinitialization");
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
395bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  this->Target = &Target;
396bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
397bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  ABI.reset(createCXXABI(Target));
398bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
399bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
4055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
40615b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
4085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
4161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
42864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
4292df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
4302df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
4312df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
4322df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
4333f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
434d3d77cd138f8e830f6547b6f83fcd5721ccf5f5dEli Friedman    if (TargetInfo::isTypeSigned(Target.getWCharType()))
4353f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
4363f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    else  // -fshort-wchar makes wchar_t be unsigned.
4373f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
4383f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  } else // C99
4393a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
44064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
441f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
442f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
443f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
444f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
445f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
446f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
447f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
448f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
449f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
450f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
451898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
452898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
453898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
454898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
455898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
456898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
4578e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4582a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
4592a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
4602a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
461864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
462864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
463864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
4643c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  // Placeholder type for pseudo-objects.
4653c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  InitBuiltinType(PseudoObjectTy,      BuiltinType::PseudoObject);
4663c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
4671de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
4681de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
4691de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
4700ddaeb9b031070ec64afe92d9892875ac44df427John McCall  // Placeholder type for unbridged ARC casts.
4710ddaeb9b031070ec64afe92d9892875ac44df427John McCall  InitBuiltinType(ARCUnbridgedCastTy,  BuiltinType::ARCUnbridgedCast);
4720ddaeb9b031070ec64afe92d9892875ac44df427John McCall
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
4755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
4778e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4787e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48013dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
481de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
482de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
48313dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
48414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
485a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
48833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
4896e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
4906e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
4916e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
492aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
493aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
494aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  InitBuiltinType(HalfTy, BuiltinType::Half);
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
497d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine &ASTContext::getDiagnostics() const {
49878a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
49978a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
50078a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
5016320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
5026320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
5036320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
5046320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
5056320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
5066320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
5076320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
5086320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
5096320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
5106320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
5116320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
5126320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
5136320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
5146320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
5156320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
5166320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
5176320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
5186320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
5196320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
520251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
521663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
5227caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
523663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
5247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
5257caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
5267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
5297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
5307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
532251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
5339421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
5349421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
5357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
5367caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
5377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
5387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
539251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
5409421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
5417caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
5427caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
543af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *ASTContext::getClassScopeSpecializationPattern(
544af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                                     const FunctionDecl *FD){
545af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
546af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
5470d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    = ClassScopeSpecializationPattern.find(FD);
5480d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  if (Pos == ClassScopeSpecializationPattern.end())
549af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return 0;
550af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
551af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return Pos->second;
552af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
553af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
554af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichetvoid ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
555af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                        FunctionDecl *Pattern) {
556af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
557af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Pattern && "Class scope specialization pattern is 0");
5580d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  ClassScopeSpecializationPattern[FD] = Pattern;
559af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
560af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
5617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
562ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
5637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
564ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
565ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
5660d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
5690d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
5700d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
5710d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
572ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
573ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
574ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
575ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
576ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
577ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
578ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
579ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
580ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
581ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
582ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
583ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
584ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
585ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
586ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
587ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
588ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
589ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
590ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
591ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
592ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
593ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
594ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
595ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
5960d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
5970d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
598d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
599d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
600d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
601d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
602d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
604d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
605d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
606d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
607d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
608d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
609d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
610d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
611d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
612d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
614d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
615d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
616d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
61714d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
61814d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
61914d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
620a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0);
62114d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
62214d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
623340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
624340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
625340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
626a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0 &&
627a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this) != 0);
628340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
629340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
6309b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
6319b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
6329b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
633a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) &&
634a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
6359b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
6369b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
637dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD,
63852bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
63952bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
640a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
64152bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
64252bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
643dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD,
64452bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
64552bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
646a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this));
64752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
64852bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
6497d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
6507d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
6517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
6527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
6537d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
6547d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
6557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6567d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
6577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
6607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
6617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
6627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
6637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
6647d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
6657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
6677d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6687d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
669c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
670c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
671c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
672c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    = OverriddenMethods.find(Method);
673c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
674c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
675c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
676c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
677c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
678c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
6797d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
6807d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
6817d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
6827d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6837d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
684464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
685464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
686464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
687a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
688b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
689b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
690b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
691183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
692b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
693b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
694b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a floating point type!");
695aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return Target->getHalfFormat();
696bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Float:      return Target->getFloatFormat();
697bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Double:     return Target->getDoubleFormat();
698bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::LongDouble: return Target->getLongDoubleFormat();
699b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
700b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
701b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
7028b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
703af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
704af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
7055d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
7065d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
7074ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
708bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  unsigned Align = Target->getCharWidth();
709dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
7104081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
7114081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
7124081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
7134081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
7144081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
7154081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
7164081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
7174081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
71882d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    // It is an error for alignas to decrease alignment, so we can
7194081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
7204081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
7214081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
7224081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
7234081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
7244081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
7254081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
7264081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
72778a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
72878a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
72978a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
73078a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
731dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
732ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
733ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
7344081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
735ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
736ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
7374081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
738af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
7396217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
7405d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
7415d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
7425d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
7435d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
7445d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
7455d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
7463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
7473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
748bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      unsigned MinWidth = Target->getLargeArrayMinWidth();
7493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
7503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
7513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
752bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
7533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
7543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
755bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
7563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
7573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
7583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
7596deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
7609f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
761dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
762ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
763ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
764ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
765ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
766ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
767ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
768ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
769ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
770ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
771ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
772ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
773dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
774ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
775ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
776ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
777ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
778ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
779ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
780ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
781ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
782ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
783ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
784ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
785ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
78605f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
787af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
788dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
789eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
790af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
791b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
792ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
793bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
794ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
795eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
796eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
797ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
798ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
799ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
800bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
801ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
802ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
803ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
804a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
805a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
8060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
8070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
8080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
8090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
810d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
8114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getTypeInfo(const Type *T) const {
8125e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
8135e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
814a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
81572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
81672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
81718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
81872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
81972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
820d3d49bb27c7ffd9accc0a6c00e887111c0348845John McCall    llvm_unreachable("Should not see dependent types");
82172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
82272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
8235d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
8245d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
82518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
82618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
82718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
82818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
82918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
83072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
831fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
83218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
83318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
83418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
83518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
836fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
8371d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
8381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83998be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
8409e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
841030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
842cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
843030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
8445c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
845213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
846030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
8479fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
8489fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
8499fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
8504bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
8516fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
8526fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
8538eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
8549fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
8559fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
8569fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
857030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
858030d8846c7e520330007087e949f621989876e3aChris Lattner  }
8595d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
8609e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
861a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
862b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unknown builtin type!");
863d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
86418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
86518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
86618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
86718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
86818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
8696f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
870bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getBoolWidth();
871bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getBoolAlign();
8726f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
873692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
874692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
875692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
8766f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
877bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getCharWidth();
878bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getCharAlign();
8796f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8803f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
8813f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
882bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getWCharWidth();
883bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getWCharAlign();
88464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
885f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
886bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar16Width();
887bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar16Align();
888f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
889f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
890bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar32Width();
891bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar32Align();
892f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
893692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
8946f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
895bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getShortWidth();
896bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getShortAlign();
8976f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
898692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
8996f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
900bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getIntWidth();
901bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getIntAlign();
9026f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
903692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
9046f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
905bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongWidth();
906bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongAlign();
9076f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
908692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
9096f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
910bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongLongWidth();
911bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongLongAlign();
9126f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
913ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
914ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
915ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
916ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
917ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
918aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
919aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Width = Target->getHalfWidth();
920aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Align = Target->getHalfAlign();
921aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
9226f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
923bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getFloatWidth();
924bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getFloatAlign();
9256f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9266f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
927bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getDoubleWidth();
928bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getDoubleAlign();
9296f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9306f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
931bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongDoubleWidth();
932bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongDoubleAlign();
9336f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9346e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
935bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
936bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0); //   == sizeof(void*)
9371590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
938e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
939e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
940e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
941bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0);
942bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0);
943e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
944a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
945bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
946d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
947bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(0);
948bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(0);
9496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
950485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
951207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
952207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
953bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
954bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
955485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
956485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
9575d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
9585d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
9595d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
9605d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
961207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
962207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
963bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
964bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
9655d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
9665d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
967f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
968207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
969bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
970bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
971f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
972f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
973f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
974071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
9761cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
977071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
9781cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
9791cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
980f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
9815d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
9825d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
9835d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
98598be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
9869e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
9875d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
9885d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
9895d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
990c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
991c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
99244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
9931d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
99444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
995dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
996dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
99744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
99844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
99972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
100072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
10011d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
10021d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
10031d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
100422ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
100522ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
10068389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
10078389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
10081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10091d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
10107176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
10117176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
10121d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
10137176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
1014dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1015dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
1016dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
1017a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
10187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
10199fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
102049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
102149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
102249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
102334b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
102434b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
102534b41d939a1328f484511c6002ba2456db879a29Richard Smith    assert(A->isDeduced() && "Cannot request the size of a dependent type");
1026dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
102734b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
102834b41d939a1328f484511c6002ba2456db879a29Richard Smith
1029075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
1030075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1031075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
103218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
1033162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1034df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
1035df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1036c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
1037c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
1038c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
1039c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
1040c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
1041c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
1042c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
1043df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
10447532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
10457176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
104618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
104718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
104818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
104918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
105018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
105118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
105218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
105318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1054395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
1055395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
1056395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
1057395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
1058ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1059ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
1060ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1061465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
1062465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10649d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
10659d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
10669d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
10679d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
10683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
10691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
107018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
10713e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
10723e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
10733e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
10743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
10753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
10763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
10773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
10783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
10793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1080b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
10812be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    std::pair<uint64_t, unsigned> Info
10822be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      = getTypeInfo(cast<AtomicType>(T)->getValueType());
10832be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Width = Info.first;
10842be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Align = Info.second;
10852be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() &&
10862be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman        llvm::isPowerOf2_64(Width)) {
10872be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // We can potentially perform lock-free atomic operations for this
10882be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // type; promote the alignment appropriately.
10892be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // FIXME: We could potentially promote the width here as well...
10902be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // is that worthwhile?  (Non-struct atomic types generally have
10912be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // power-of-two size anyway, but structs might not.  Requires a bit
10922be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // of implementation work to make sure we zero out the extra bits.)
10932be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      Align = static_cast<unsigned>(Width);
10942be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    }
1095b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1096b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
109718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10992be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman  assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2");
11009e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1101a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1102a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1103eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1104eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1105eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1106eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1107eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1108dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1109dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1110dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1111dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1112dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1113bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1114bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
11154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1116eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1117bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
11184ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1119eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1120bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1121bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
112216e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
112386fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
11244ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1125eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
112686fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
11274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1128eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
112986fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
113086fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
113134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
113234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
113334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
113434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
11354ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
113634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
11371eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
11381eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1139183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
11401eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
11411eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
11421eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
11431eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
11441eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
114534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
114634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
114734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
11482c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
11492c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
11502c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
11512c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
11522c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
11539820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
11542c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
11552c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
1156db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose                            SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
11572c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
11582c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
11592c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
11602c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
11612c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
116211062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian      Ivars.push_back(*I);
11633060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1164bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian    ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1165db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1166bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian         Iv= Iv->getNextIvar())
1167bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian      Ivars.push_back(Iv);
1168bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  }
11699820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
11709820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1171e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1172e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1173e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1174432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1175e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
117653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
117753b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
117853b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
117953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1180e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1181432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1182e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1183b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
1184b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian        Protocols.insert(*P);
1185e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1186e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1187b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1188e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1189e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1190e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1191e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1192e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
1193e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1194e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1195e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1196e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1197e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1198b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
119953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1200e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1201e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1202432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1203e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1204e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1205e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1206e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1207b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1208e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1209e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1210e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1211432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1212e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1213e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1214e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1215e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1216e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1217e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1218e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
12194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
12203bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
12213bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
122280aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
122380aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian       CDecl = CDecl->getNextClassExtension())
1224b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
1225b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
12263bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
12273bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
12283bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1229b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1230b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
12318e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
12328e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
12338e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
12348a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
12358a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
12368a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
12378a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
12388a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
12398a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
12408a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
12418a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12428a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
12438a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
12448a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
12458a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
12468a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
12478a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
12488a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
12498a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12508a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
12518a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
12528a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
12538a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
12548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
12558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
12568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
12588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
12598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
12608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
12618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
12628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
12641ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
12651ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1266830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1267d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1268d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1269d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1270830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1271d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
12721ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
12731ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
12741ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
12751ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
12761ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
12771ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1278d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1279d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
12801ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
12811ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
12821ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1283a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Allocate an uninitialized TypeSourceInfo.
1284b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1285a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// The caller should initialize the memory held by TypeSourceInfo using
1286b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
1287b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1288b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
1289b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
1290b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
1291a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
12924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1293109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1294109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1295109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1296109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1297a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1298109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1299a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1300a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1301a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1302a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1303b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1304b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1305a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
13066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1307a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1308c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1309a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1310a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1311a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1312b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
13134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1314b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1315b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1316b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1317b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
13184ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
13194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1320b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1321b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1322b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1323a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1324a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1325a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1326a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
13274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
13283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
13293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
13303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
13310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
13330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
13343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
13353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
13363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
13373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
13383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
13390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
13400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
13423b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
13433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
13443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
13453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonSplit.second.addConsistentQualifiers(quals);
13463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getExtQualType(canonSplit.first, canonSplit.second);
13473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
13483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
13493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
13503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
13513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
13523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
13533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
13543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
13550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
13560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13574ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
13584ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
1359f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1360f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1361f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1362b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
13630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
13640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
13650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
13660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
13690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
13700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
13710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
13720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1375ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1376ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1377b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
13784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
1379d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1380b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1381d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
13821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13837f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
13847f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
138558f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
13864027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
13874027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
13884027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
13894027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
13901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
13920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
13930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
13940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
13951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
13970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
13980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
13990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
14000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1403d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1404a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1405e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1406e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
1407e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
1408e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
1409e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1410e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
1411e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1412e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1413e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
1414e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1415e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1416e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
1417e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1418e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                             FPT->getNumArgs(), EPI);
1419e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
1420e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1421e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
1422e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
1423e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
14245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
14255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
14264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
14311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
14351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1439467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1440f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
14411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1444c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14466b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
14495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
14544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1467467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1468f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1472c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14746b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
14815618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
14824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
1483296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1484296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
14855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
14865618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
14875618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
14881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14895618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
14905618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
14915618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
14925618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
14931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
14955618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
14965618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1497467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
14985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
15015618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
15025618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1503c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
15056b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
15066b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
15075618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
15085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
15095618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
15105618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
15115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
15127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
15137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
15144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
15154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
15169625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
15179625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
15189625e44c0252485277a340746ed8ac950686156fDouglas Gregor
15195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
15205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
152254e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
15267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
15275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
15287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
152954e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
153054e14c4db764c0636160d26c5bbf491637c83a76John McCall
15315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
15335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
153454e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
153554e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
153654e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
15377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
15397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
15407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1541c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
15437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15446b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
154554e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
154654e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
15477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
15487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
154954e14c4db764c0636160d26c5bbf491637c83a76John McCall
15507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
15517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
15527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
15547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
15554ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
15567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
15577c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
15587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
155954e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
15607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
15627c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
15637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
15647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
15657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
156654e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
156754e14c4db764c0636160d26c5bbf491637c83a76John McCall
15687c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
15697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
15707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
157154e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
157254e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
157354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
15747c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
15767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
15777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1578c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
15826b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15847c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1588f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1589f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
15904ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
1591f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1592f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1593f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1594f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1595f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1596f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1597f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1598f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1599f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1600f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1601f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1602f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1603f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
160487c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1605f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1606f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1607f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1608f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1609f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1610c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
1611f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
16126b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
16136b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1614f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1615f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1616f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1617f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1618f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1620fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
16211eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
162238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1623c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
162463e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
1625923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1626923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1627587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1628587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
162938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
163038aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
163138aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
16329f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
1633bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
16341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
163663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
16407192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
16415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
16421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
16443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
16453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
16463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
16473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
16483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize,
164963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
16503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
16513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
16525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
16531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
16547192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1655c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16586b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
165963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
16607192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
16615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1665ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
1666ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
1667ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
1668ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1669ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
1670ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
1671ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1672ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
1673ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1674ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
1675ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  const Type *ty = split.first;
1676ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
1677ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
1678ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
1679ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1680ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
1681ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
1682ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1683ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
1684ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
1685ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
1686ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
1687ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
1688ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
1689ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
1690ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
1691ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
1692ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
1693ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
1694ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
1695ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
1696ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
1697ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
1698ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1699ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
1700ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
1701ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
1702ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
1703ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
1704ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
170534b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
1706ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
1707ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
1708ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1709ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
1710ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
1711ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
1712ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
1713ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
1714ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
1715ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
1716ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1717ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
1718ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
1719ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
1720ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
1721ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
1722ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
1723ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
1724ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1725ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1726ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
1727ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1728ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
1729ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
1730ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
1731ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1732745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1733ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1734ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
1735ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1736ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
1737ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
1738ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1739745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1740745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1741b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
1742b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    const AtomicType *at = cast<AtomicType>(ty);
1743b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
1744b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
1745b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1746b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
1747ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
1748ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1749ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
1750ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
1751ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
1752ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
1753ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
1754ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1755745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1756745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1757ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
1758ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1759ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
1760ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
1761ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
1762ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
1763ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
1764ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
1765ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1766ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1767ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1768ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
1769ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
1770ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1771ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1772ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
1773ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1774ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
1775ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
1776ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
1777ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1778ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1779ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1780ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
1781ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
1782ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
1783ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1784ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
1785ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1786ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
1787ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
1788ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
1789ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1790ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1791ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1792ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1793ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
1794ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  return getQualifiedType(result, split.second);
1795ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
1796745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1797bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1798bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
17997e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
18007e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1801c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
180263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
18034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
1804c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1805c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
18063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
1807715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
18083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
18093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
18103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
18113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM,
181263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
18133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
1814715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
1815715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
18166b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
181763e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
1818c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1819c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1820c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1821c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1822c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1823c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1824898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1825898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
182604d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
18273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
18283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
1829898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
18303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
18313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
18323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
18333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
1834898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1835898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
18363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
18373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
18383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
18393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
18403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
18413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
18423b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
18433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
18443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
18453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
18463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
18473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
1848cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
1849cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
18503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
18513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
18521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
1854898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
18553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
18563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
18573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
18583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   QualType(canonElementType.first, 0),
18593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
18603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
18623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
18633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
18643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
18663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
18673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
18683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      DependentSizedArrayType(*this, QualType(canonElementType.first, 0),
18693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
18703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
18713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
18723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
18733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
18743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
18763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
18773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                    canonElementType.second);
18783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
18803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
18813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (QualType(canonElementType.first, 0) == elementType)
18823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
18833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
18853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
18863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
18873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
18883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
18893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
18903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
18913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
18923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
18933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
1895c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
18963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
1897c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
18983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
1899c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
19013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
19023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
19033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
1904c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1905c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
19063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
19073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
19083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
1909c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
19113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
19123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getIncompleteArrayType(QualType(canonSplit.first, 0),
19133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
19143b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getQualifiedType(canon, canonSplit.second);
1915c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1916c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
19173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
19183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
19193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
19202bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1921c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
19233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
1924c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
19263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
19273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
1928fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1929fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
193073322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
193173322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
193282287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
19334ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
19343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
19375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1938e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
1939788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
19405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
19415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
19425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
19435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
19455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
19465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1947255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
1948231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
19491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
19515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1952c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
19535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19546b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
1955e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
19565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
19575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
19585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
19595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1961213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
196273322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
19634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19644ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
19654ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  assert(vecType->isBuiltinType() || vecType->isDependentType());
19661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
196773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
196873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
1969788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
1970e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
197173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
197273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
197373322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
197473322924127c873c13101b705dd823f5539ffa5fSteve Naroff
197573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
197673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
197773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
1978467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
1979213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198173322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
198273322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1983c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
198473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
19856b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
19866b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
198773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
198873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
198973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
199073322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
199173322924127c873c13101b705dd823f5539ffa5fSteve Naroff
19924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
19944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
19954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
19962ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
19971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
19982ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
19991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20002ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
20012ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
20022ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
20032ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
20042ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
20052ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
20062ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
20076b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
20086b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
20096b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
20102ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
20112ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
20122ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
20136b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
20146b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
20156b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
2016789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2017789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
2018789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2019789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
2020789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
20212ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
20222ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
20232ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
20242ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
20256b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
20266b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
20272ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
20282ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
20291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20309cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
20319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
20329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
20339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
203472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
20355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
20364ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
20374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
20384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
2039cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
2040cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2041cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
20425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
20435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
20445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2045264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
20461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
20481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
204972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
20511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2053ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
205404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
2055264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
2056264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
2057264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
20581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
206072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
206172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2062c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2065cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
20666b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
2067cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
206972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
20754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
20764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionType(QualType ResultTy,
20774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const QualType *ArgArray, unsigned NumArgs,
20784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
20805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
20815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
20828026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
20835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
208672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
20875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
2088465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2089465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
20908b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl  bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical();
20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
209254e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
20945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2095cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2096cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2097cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2098e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2100465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
210204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
21035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 16> CanonicalArgs;
21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
21055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
210654e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2107465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2108e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
21098b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
21108b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2111e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2112e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2113e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2114f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
2115beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
2116e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                CanonicalEPI);
2117465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
21185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
211972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
212072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2121c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2123465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2124f85e193739c953358c865005855253af4f68a497John McCall  // FunctionProtoType objects are allocated with extra bytes after
2125f85e193739c953358c865005855253af4f68a497John McCall  // them for three variable size arrays at the end:
2126f85e193739c953358c865005855253af4f68a497John McCall  //  - parameter types
2127f85e193739c953358c865005855253af4f68a497John McCall  //  - exception types
2128f85e193739c953358c865005855253af4f68a497John McCall  //  - consumed-arguments flags
2129f85e193739c953358c865005855253af4f68a497John McCall  // Instead of the exception types, there could be a noexcept
2130f85e193739c953358c865005855253af4f68a497John McCall  // expression.
2131e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
213260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
213360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (EPI.ExceptionSpecType == EST_Dynamic)
213460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
213560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
21368026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
213760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2138f85e193739c953358c865005855253af4f68a497John McCall  if (EPI.ConsumedArguments)
2139f85e193739c953358c865005855253af4f68a497John McCall    Size += NumArgs * sizeof(bool);
2140f85e193739c953358c865005855253af4f68a497John McCall
2141e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2142cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2143cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
21448026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
21455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
214672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
21475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
21485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
21513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
21523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
21533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
21543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
21553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
21563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
21573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
21583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
21593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
21603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
21613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
21623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
21633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
21643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
21653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
21664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
21673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
21683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
21693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
217037ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
21713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
21723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
21733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
21743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2175f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
217631f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2177f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2178f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
21793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
21803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
21813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
21823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
21832ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
21842ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
21854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
21861e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2187becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
21881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2189162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
21902ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2191becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2192becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2193becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2194becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
219519c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2196becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Record->getPreviousDeclaration() &&
2197becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2198becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2199400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
220019c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2201becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Enum->getPreviousDeclaration() &&
2202becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2203400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
220419c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2205ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2206f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2207f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2208f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
22099fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2210becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
221149aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
221249aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
22132ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
22142ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2216162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
22179763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2218162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2219162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
22211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22229763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
22239763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2224f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
22256b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2226f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2227f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2228f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
22295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2232400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2233400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2234400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
2235400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2236400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2237400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2238f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2239f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2240f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2241f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2242400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2243400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
22444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2245400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2246400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2247400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
2248400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2249400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2250400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2251f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2252f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2253f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2254f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2255400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2256400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
22579d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
22589d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
22599d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
22609d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
22619d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
22629d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22639d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
22649d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
22659d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
22669d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22679d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
22689d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
22699d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
22709d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22719d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
22729d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
22739d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22749d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
22759d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
22769d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22779d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
227849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
227949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
228049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
22814ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2282467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
228349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
228449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
228549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
228649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
228749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
228849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
228949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
229049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
229149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
229249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
229349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
229449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
229549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
229649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
229749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
229849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
229949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
230049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
2301c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
2302c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
2303c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
2304c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
2305c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
2306c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2307c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
2308c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
2309c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2310c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2311c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2312c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
2313c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2314c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
2315c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2316c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
2317c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
2318c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2319c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
2320c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2321c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
2322c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
2323c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
2324c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2325c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
2326c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2327c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2328c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2329c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
2330c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2331c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
2332c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
2333c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2334c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
2335c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
2336c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2337fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
23381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
233976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
23401eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
234176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
23424fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
2343fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
23444fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
2345fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
23461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
2347fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2348fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2349fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
2350fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
23511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23524fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
235376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
23544fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
2355789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2356789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
2357789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2358789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
2359789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
236076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
23616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
23626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
2363fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2364fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
2365fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2366fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2367fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
2368fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
2369fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
23703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
23713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
23723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
23733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
23743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
23757c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
23767c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
23773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
23783cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
23793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
23803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
23813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
23823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
23833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
23843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
23853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
23863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
23873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
23883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
23893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
23901eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
23917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
2392d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
23933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
23947c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
23957c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
23967c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2397d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
2398d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
23995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> ArgVec;
2400833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
2401833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
2402833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
2403833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
240431f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
24053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
2406833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
2407833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2408833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
2409833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
24107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
24117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
24123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
24137c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
24147c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
24150f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
24160f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
24170f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
24187c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
24193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  bool isTypeAlias =
24203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
24213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
24223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
24233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
24243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
24253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
24263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
24273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    assert(!isTypeAlias &&
24283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith           "Underlying type for template alias must be computed by caller");
24293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
24303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
24313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2432fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
24331275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
24341275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
24351275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
24363e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
24373e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
24383e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       (isTypeAlias ? sizeof(QualType) : 0),
24396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
24401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
2441ef99001908e799c388f1363b1e607dad5f5b57d3John McCall    = new (Mem) TemplateSpecializationType(Template,
244231f17ecbef57b5679c017c375db330546b7b5145John McCall                                           Args, NumArgs,
24433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                           CanonType,
24443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                         isTypeAlias ? Underlying : QualType());
24451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
244655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
24471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
244855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
244955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
24501eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
24519763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
24529763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
24534ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
24547c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
24557c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
24563e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  assert((!Template.getAsTemplateDecl() ||
24573e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) &&
24583e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith         "Underlying type for template alias must be computed by caller");
24593e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
24600f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
24610f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
24620f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
24637c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
24649763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
24659763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
24665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> CanonArgs;
24679763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
24689763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
24699763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
24709763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24719763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
24729763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
24739763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
24749763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
24759763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
24769763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24779763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
24789763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
24799763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
24809763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24819763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
24829763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
24839763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
24849763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
24859763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
24869763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
24879763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
24883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
24899763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
24909763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
24919763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
24929763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24939763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
24949763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
24959763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
24969763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
24979763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24989763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2499465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2500465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
25014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
2502e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
2503465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
2504e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2505e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
2506465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2507e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
2508e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
2509e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2510789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
2511789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
2512789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
2513465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2514465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
2515789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
2516789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
2517789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2518465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
2519e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
2520465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
2521e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
2522e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
2523e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2524075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
25254ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
2526075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
2527075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
2528075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2529075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
2530075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2531075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
2532075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
2533075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2534075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
2535075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
2536075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
2537075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2538075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
2539075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
2540075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
2541075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2542075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
2543075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
2544075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
2545075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
2546075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
2547075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
25484a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
25494a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
25504a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
25514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
2552d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2553d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2554d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
2555d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
25564a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
25574a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
25584a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
25594a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
25604a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
25614a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
2562d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2563d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2564d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
25654a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
2566d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2567d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
25684714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
25694714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2570d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
2571d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
2572d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
25734a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
2574d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
25754714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
25761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
2577d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2578d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
25791eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
258033500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
258133500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
25824a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
258333500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
25844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
258533500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
25865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> ArgCopy;
258733500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
258833500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
258933500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
259033500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
259133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
259233500955d731c73717af52088b7fc0e7a85681e7John McCall}
259333500955d731c73717af52088b7fc0e7a85681e7John McCall
259433500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
259533500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
259633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
259733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
259833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
259933500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
26004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
2601aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
2602aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
26031734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2604789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
260533500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
260633500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
2607789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2608789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
260933500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
261033500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2611789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
2612789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
2613789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
261433500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
261533500955d731c73717af52088b7fc0e7a85681e7John McCall
261633500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
261733500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
26181734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
261933500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
26205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
262133500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
262233500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
262333500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
262433500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
2625789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
26261734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
262733500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
262833500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
262933500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
263033500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
263133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
263233500955d731c73717af52088b7fc0e7a85681e7John McCall
263333500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
263433500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
263533500955d731c73717af52088b7fc0e7a85681e7John McCall  }
263633500955d731c73717af52088b7fc0e7a85681e7John McCall
263733500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
263833500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
263933500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
2640ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
264133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
26421734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
264333500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
26441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
26451734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
26461734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2647cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
2648cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                      llvm::Optional<unsigned> NumExpansions) {
26497536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
2650cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
26517536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26527536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
26537536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
26547536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
26557536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
26567536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
26577536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
26587536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
26597536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26607536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
26617536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
2662cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
26637536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26647536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    // Find the insert position again.
26657536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
26667536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
26677536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
2668cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
26697536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
26707536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
26717536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
26727536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
26737536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
267488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
267588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
267688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
267788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
26782e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
267988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
268088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2681c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
268254e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
268354e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
268454e14c4db764c0636160d26c5bbf491637c83a76John McCall
268554e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
268654e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
268754e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
268854e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
268954e14c4db764c0636160d26c5bbf491637c83a76John McCall}
269054e14c4db764c0636160d26c5bbf491637c83a76John McCall
269154e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
269288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
269388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
26941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
269588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
269688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
269788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
269888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
269988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
270088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
270188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
270288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2703c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
2704c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
27054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
2706c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
2707c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
2708c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2709c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
2710d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2711c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
2712c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
2713c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
2714d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
2715c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2716c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
2717d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2718c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
2719c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
272054e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2721c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2722c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
2723c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
27245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
27250237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
272654e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
272754e14c4db764c0636160d26c5bbf491637c83a76John McCall
272854e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2729c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2730c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
273154e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
2732c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2733c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
273454e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
273554e14c4db764c0636160d26c5bbf491637c83a76John McCall
273654e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
2737c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
273854e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
273954e14c4db764c0636160d26c5bbf491637c83a76John McCall
2740c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
2741c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2742fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2743c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
2744c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
27451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2746c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2747c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
2748c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2749d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
275088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2751c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2752c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
27534ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
27544b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2755c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
27561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27574b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2758c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
2759c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
27604b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
27611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2762c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
276354e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2764c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
2765c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
276654e14c4db764c0636160d26c5bbf491637c83a76John McCall
2767c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
2768c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
276954e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
277054e14c4db764c0636160d26c5bbf491637c83a76John McCall
2771c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
2772c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2773c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
2774c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
277524fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
277624fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
2777c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
277824fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
277924fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
278024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
2781deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
2782deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
27834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const {
2784deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
2785deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
278674c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
2787deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  // FIXME: redeclarations?
2788deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2789deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2790deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
2791deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
2792deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
2793c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
2794c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
279572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
279672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
27979752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
27981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
27999752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
28004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
2801dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2802b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2803b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2804b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
28051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2806b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2807b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2808b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2809b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2810b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2811b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
28126b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
2813b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
28143060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
2815b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
28166b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
28176b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
2818b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2819b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2820b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2821b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2822dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
28236b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
2824dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
28259752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
28269752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2827d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2828d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
28299752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
28309752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
28319752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
28321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
28339752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
28344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
2835f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
28366b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
28379752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
28389752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2839d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2840d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
284160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
284260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
28434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
2844a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2845a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
28461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
284760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
284860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
284960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
285060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
285160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
285260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
285360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
285460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
285560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
285660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
285760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
285860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
285960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
286060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
286160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
28621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
286360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
28641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
286660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
28677eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  if (e->isLValue())
286860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
28691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
287060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
287160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
287260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2873395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2874395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2875395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
28761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2877395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
28784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getDecltypeType(Expr *e) const {
2879dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
2880561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
2881561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  // C++0x [temp.type]p2:
2882561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   If an expression e involves a template parameter, decltype(e) denotes a
2883561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   unique dependent type. Two such decltype-specifiers refer to the same
2884561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   type only if their expressions are equivalent (14.5.6.1).
2885561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (e->isInstantiationDependent()) {
28869d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
28879d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
28881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28899d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
28909d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
28919d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
28929d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
28939d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
28949d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
28956b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
28969d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
28973060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
28989d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
28996b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
29009d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
29019d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
29029d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
29039d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2904dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
29056b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
2906dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2907395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2908395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2909395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2910395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
2911ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
2912ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
2913ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
2914ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
2915ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
2916ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
2917ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
291869d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor    new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
291969d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                                   Kind,
292069d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                 UnderlyingType->isDependentType() ?
292169d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                    QualType() : UnderlyingType);
2922ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
2923ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
2924ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
2925ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
2926483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith/// getAutoType - We only unique auto types after they've been deduced.
292734b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTContext::getAutoType(QualType DeducedType) const {
2928483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
2929483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (!DeducedType.isNull()) {
2930483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    // Look in the folding set for an existing type.
2931483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    llvm::FoldingSetNodeID ID;
2932483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoType::Profile(ID, DeducedType);
2933483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2934483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith      return QualType(AT, 0);
2935483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  }
2936483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
2937483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2938483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
2939483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
2940483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
2941483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
294234b41d939a1328f484511c6002ba2456db879a29Richard Smith}
294334b41d939a1328f484511c6002ba2456db879a29Richard Smith
2944b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// getAtomicType - Return the uniqued reference to the atomic type for
2945b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// the given value type.
2946b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType ASTContext::getAtomicType(QualType T) const {
2947b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Unique pointers, to guarantee there is only one pointer of a particular
2948b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // structure.
2949b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  llvm::FoldingSetNodeID ID;
2950b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType::Profile(ID, T);
2951b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2952b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  void *InsertPos = 0;
2953b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
2954b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return QualType(AT, 0);
2955b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2956b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // If the atomic value type isn't canonical, this won't be a canonical type
2957b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // either, so fill in the canonical type field.
2958b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  QualType Canonical;
2959b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T.isCanonical()) {
2960b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Canonical = getAtomicType(getCanonicalType(T));
2961b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2962b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Get the new insert position for the node we care about.
2963b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
2964b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
2965b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
2966b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
2967b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  Types.push_back(New);
2968b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicTypes.InsertNode(New, InsertPos);
2969b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return QualType(New, 0);
2970b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
2971b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2972ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
2973ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
2974ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
2975ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoDeductTy = getAutoType(QualType());
2976ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
2977ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
2978ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
2979ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
2980ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
2981ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
2982ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
2983ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
2984ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
2985ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
2986ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
2987ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
29885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
29895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
29904ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
2991d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2992e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2993e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2994e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
29955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
29965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
29971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
29981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
29991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
3000a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
3001bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getSizeType());
30025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
30035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
300464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
300564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
300664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
300764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
300864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
300964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
301064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
301164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
301264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
301364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
301464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
301564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
301664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
301764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
30188b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
30198b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
30208b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
3021bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getPtrDiffType(0));
30228b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
30238b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
3024e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3025e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
3026e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3027e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
30284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
302954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
303054e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
303154e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
3032745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
303354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
303454e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
303554e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
303654e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
303754e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
303854e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
303954e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
304054e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
304154e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
304254e14c4db764c0636160d26c5bbf491637c83a76John McCall
304354e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
304454e14c4db764c0636160d26c5bbf491637c83a76John McCall}
304554e14c4db764c0636160d26c5bbf491637c83a76John McCall
304662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
304762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
304862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
304962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
305062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
305162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
305262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
305362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
305462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
305562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
305662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
305762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
30589dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
305962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
306062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
306128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
306228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
306362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
306462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
306562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
306662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
306762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
306862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
306962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
307062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
307162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
307262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
307362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
307462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
307562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
307662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
307762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  quals.addConsistentQualifiers(splitType.second);
307828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
30799dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
308062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
308128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
308228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
308328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
30849dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
308562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
308628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
308728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
30889dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
308962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
30903fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
30919dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
30929dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
30939dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
30949dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
30959dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
30969dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
309762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
309828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
309928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
310028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
310128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
31025a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
31035a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
31045a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
31055a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
31065a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
31075a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
31085a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
31095a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
31105a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
31115a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
31125a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
31135a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
31145a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
31155a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
31165a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
31175a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
31185a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31195a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
31205a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
31215a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
31225a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
31235a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
31245a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
31255a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
31265a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
31275a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
31285a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31295a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (getLangOptions().ObjC1) {
31305a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
31315a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
31325a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
31335a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
31345a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
31355a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
31365a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
31375a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
31385a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31395a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
31405a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31415a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
31425a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
31435a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
31454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
31464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
3147146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3148146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3149146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template:
31502577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
3151146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
3152146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
31532577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3154146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate: {
3155146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
3156146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    // DNInfo work in progress: CHECKME: what about DNLoc?
3157146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
3158146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3159146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3160146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3161146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
31622577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
316380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
31642577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
31652577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
316680ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
31672577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
31682577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
31692577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
31702577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
31712577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
31722577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
317380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
317480ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
317580ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
3176146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3177146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3178146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3179146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameter()->getDeclName(),
3180146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3181146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3182146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3183146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3184146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3185146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParmPack();
3186146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
3187146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3188146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3189146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3190146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3191146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name kind!");
319280ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
319380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
31944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
3195146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3196146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3197146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template: {
3198146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateDecl *Template = Name.getAsTemplateDecl();
31993e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
3200146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          = dyn_cast<TemplateTemplateParmDecl>(Template))
32013e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
32023e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
32033e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
320497fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
32053e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
320625a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3207146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate:
3208146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    llvm_unreachable("cannot canonicalize overloaded template");
3209146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3210146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3211146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3212146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    assert(DTN && "Non-dependent template names must refer to template decls.");
3213146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DTN->CanonicalTemplateName;
3214146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3215146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3216146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3217146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3218146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3219146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getCanonicalTemplateName(subst->getReplacement());
32201aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
32211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3222146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3223146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3224146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                  = Name.getAsSubstTemplateTemplateParmPack();
3225146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *canonParameter
3226146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
3227146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateArgument canonArgPack
3228146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateArgument(subst->getArgumentPack());
3229146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
3230146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3231146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3232146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3233146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name!");
323425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
323525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3236db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3237db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3238db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3239db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3240db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3241db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
32421eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
32434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
32441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
32451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
32461275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
32471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
32491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
32501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32511275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
3252833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
32531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3254788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3255788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3256a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3257a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3258a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
3259a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
32602be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
3261a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
32621275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
3263833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(*Arg.getAsIntegral(),
32641275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
32651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32661275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
3267833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
32681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32691275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
327087dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
327187dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
327287dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
3273910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
3274910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
32751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
32761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
32771275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
32781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
32791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
32801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3281910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
32821275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
32831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
32841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
32851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
3286b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled template argument kind");
32871275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
32881275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
3289d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
32904ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
32911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
3292d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
3293d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3294d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
3295d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
3296d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
32971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
3298d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3299d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
3300d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3301d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
3302d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
3303d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
330414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
330514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
330614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
330714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
330814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
330914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
331014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
331114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
331214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
3313d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3314d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
3315d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
3316d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
3317264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3318264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
3319264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
3320264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
3321264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
3322264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
3323264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
3324264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
3325264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3326264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3327264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3328264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      return NestedNameSpecifier::Create(*this, Prefix,
3329264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3330264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3331264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3332643f84353b3b7bbf9b73bcbc7da0ef8a39a5ca99Douglas Gregor    // Do the same thing as above, but with dependent-named specializations.
3333264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentTemplateSpecializationType *DTST
3334264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor          = T->getAs<DependentTemplateSpecializationType>()) {
3335264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3336264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DTST->getQualifier());
3337aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
3338aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3339aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 Prefix, DTST->getIdentifier(),
3340aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getNumArgs(),
3341aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getArgs());
3342264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      T = getCanonicalType(T);
3343264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3344264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
33453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
33463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
3347d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3348d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3349d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
3350d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
3351d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
3352d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3353d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3354d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
3355d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
3356d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3357d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3358c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
33594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
3360c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
3361a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
3362c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
3363c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3364c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
3365c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
33661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
33683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
3369c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
33701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
3372c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
3373c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
33741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3375c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
3376c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
337750d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
33780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
33793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
33803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs = split.second;
33811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3382c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
33833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
33843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
3385c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
33861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3387c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
3388c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
33893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
33901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3391c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3392c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3393c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
33940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
3395c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3396c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3397c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
33980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
3399898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
34001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
3401898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
3402898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
34031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
34043fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
3405898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
34060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
34077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
34081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3409c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
34107e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
34113fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
3412c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
34130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
34147e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
341577c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
341677c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
341779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas GregorQualType ASTContext::getAdjustedParameterType(QualType T) {
341879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p7:
341979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "array of type" shall be
342079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   adjusted to "qualified pointer to type", where the type
342179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   qualifiers (if any) are those specified within the [ and ] of
342279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   the array type derivation.
342379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isArrayType())
342479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getArrayDecayedType(T);
342579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
342679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p8:
342779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "function returning type"
342879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   shall be adjusted to "pointer to function returning type", as
342979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   in 6.3.2.1.
343079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isFunctionType())
343179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getPointerType(T);
343279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
343379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T;
343479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
343579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
343679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas GregorQualType ASTContext::getSignatureParameterType(QualType T) {
343779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getVariableArrayDecayedType(T);
343879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getAdjustedParameterType(T);
343979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T.getUnqualifiedType();
344079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
344179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
3442e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
3443e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
3444e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
3445e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
3446e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
3447e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
34484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
3449c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
3450c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
3451c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
3452c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
3453c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3454c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
34551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3456c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
3457e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
3458e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
34590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
3460e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
3461e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
34623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
34633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
34645e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
34655e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
34663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
34673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
34683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
34693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
34703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    const ArrayType *array = split.first->getAsArrayTypeUnsafe();
34713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
34721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
34743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    qs.addConsistentQualifiers(split.second);
34753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
34761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
34786183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
34796183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
34800de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
34811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
34820de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
34830de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
34840de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
34850de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
34860de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
34870de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
34880de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
34890de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
34900de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
34915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
34925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
3493a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
3494183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
34955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
3496a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
3497183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3498183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
3499b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getFloatingRank(): not a floating type");
3500aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return HalfRank;
35015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
35025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
35035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
35045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
35055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
35065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
35081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
3509716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
3510716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
35111361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
35121361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
35131361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
35141361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
35151361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
3516b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("getFloatingRank(): illegal value for rank");
3517f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
3518f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
3519f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
3520f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
3521f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
35221361b11066239ea15764a2a844405352d87296b3Chris Lattner
35231361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
35241361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
3525b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getFloatingRank(): illegal value for rank");
35261361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
35271361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
35281361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
35295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
35305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
35315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35327cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
35337cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
35347cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
35351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
35364ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
3537a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
3538a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
35391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3540a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
3541fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
3542a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
3543fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
3544fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
35455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
35465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3547f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3548f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
3549f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
3550f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
3551467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
3552f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType* ET = dyn_cast<EnumType>(T))
3553842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    T = ET->getDecl()->getPromotionType().getTypePtr();
3554f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
35553f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
35563f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      T->isSpecificBuiltinType(BuiltinType::WChar_U))
3557bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    T = getFromTargetType(Target->getWCharType()).getTypePtr();
3558a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
3559f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
3560bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    T = getFromTargetType(Target->getChar16Type()).getTypePtr();
3561f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3562f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
3563bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    T = getFromTargetType(Target->getChar32Type()).getTypePtr();
3564f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3565f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
3566b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getIntegerRank(): not a built-in integer");
35677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
3568f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
35697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
35707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
35717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
35727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
3573f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
35747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
35757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
3576f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
35777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
35787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
3579f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
35807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
35817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
3582f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
35837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
35847cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
3585f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
35862df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
35872df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
35882df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
3589f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
3590f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
3591f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
359204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
359304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
359404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
359504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
359604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
35974ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
3598ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
3599ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
3600ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
360104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
360204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
360304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
360404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
360504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
360604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3607a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  uint64_t BitWidth = Field->getBitWidthValue(*this);
360804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
360904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
361004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
361104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
361204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
361304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
361404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
361504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
361604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
361704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
361804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
361904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
362004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
362104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
362204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
362304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
362404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3625a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
3626a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3627a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
36284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
3629a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
3630a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
3631842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
3632842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
3633a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
3634a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
3635a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
3636a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
3637a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3638a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3639a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
3640a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
364131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// \brief Recurses in pointer/array types until it finds an objc retainable
364231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// type and returns its ownership.
364331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisQualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
364431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  while (!T.isNull()) {
364531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T.getObjCLifetime() != Qualifiers::OCL_None)
364631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return T.getObjCLifetime();
364731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T->isArrayType())
364831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = getBaseElementType(T);
364931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const PointerType *PT = T->getAs<PointerType>())
365031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = PT->getPointeeType();
365131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
365228445f0b62f6aed851ff87ce64d9b19200d3211fArgyrios Kyrtzidis      T = RT->getPointeeType();
365331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else
365431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
365531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
365631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
365731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return Qualifiers::OCL_None;
365831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
365931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
36601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
36617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
36621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
36634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
3664f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3665f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
36667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
36671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3668f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3669f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
36701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
36727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
36731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
36757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
36767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
36777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
36781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
36807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
36817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
36827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
36837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
36841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
36867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
36871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
36887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
36897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
36907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
36917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
36927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
36937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
36941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
36967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
36971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
36987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
36995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
370071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
370179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
3702ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3703ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
3704ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
370579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  if (Ctx.getLangOptions().CPlusPlus)
3706ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
370779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
3708ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
370979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
3710ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
37111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
37124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
371371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
37141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
3715ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
371679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
37175cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
371879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
3719f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
37201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
37220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
3723f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
3724f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
372571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
37260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
372771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
37281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
37291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
373044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
373144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
37321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
3733ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
373444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
3735a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
37361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
37377a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*Mutable=*/false,
37387a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*HasInit=*/false);
37392888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
374017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
374144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
374244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
3743838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
374471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
37451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
374671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
37478467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
3748b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3749319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
37506217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3751319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
3752319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
3753319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3754319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
37554ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
3756adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
3757adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
3758adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3759adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3760adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3761ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
376279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
37635cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3764adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3765adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3766adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3767adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3768adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3769adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3770adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3771adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
3772083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
3773adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3774adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3775adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
3776ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3777adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
3778adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3779a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3780adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
37817a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
37827a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
37832888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3784adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3785adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3786adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3787838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3788adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3789adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
3790adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3791adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
3792adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3793adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
37944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
3795083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
3796083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
3797083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3798083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
3799083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3800ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
380179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
38025cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3803083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3804083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
3805083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3806083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3807083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
3808083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
3809083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3810083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3811083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
3812083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
3813083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
3814083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
3815083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
3816083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3817083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3818083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
3819ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3820083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
3821083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
3822a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3823083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
38247a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
38257a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
38262888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3827083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
3828083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
3829083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3830838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3831083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3832083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
3833083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3834083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
3835083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
3836083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
38374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::BlockRequiresCopying(QualType Ty) const {
3838f85e193739c953358c865005855253af4f68a497John McCall  if (Ty->isObjCRetainableType())
3839af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3840e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  if (getLangOptions().CPlusPlus) {
3841e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    if (const RecordType *RT = Ty->getAs<RecordType>()) {
3842e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3843ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt      return RD->hasConstCopyConstructor();
3844e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian
3845e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
3846e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
3847af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
3848af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
3849af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
38504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
38515f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const {
3852af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
3853ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
3854af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
3855ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
3856ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
3857a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__copy_helper;            // as needed
3858a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__destroy_help            // as needed
3859af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
3860ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
3861ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3862af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3863af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3864af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
3865f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3866f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3867f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
3868af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
3869ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
3870af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
3871af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
3872af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3873af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
3874af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3875af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
3876af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3877af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3878af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3879af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3880af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
3881af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3882af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
38835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef FieldNames[] = {
3884af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
3885af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
3886af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
3887af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
3888af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
3889af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
3890af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
3891af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3892af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3893af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
3894af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
3895af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
3896af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3897ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         SourceLocation(),
3898af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
3899a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
39007a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*BitWidth=*/0, /*Mutable=*/false,
39017a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
39022888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3903af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
3904af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
3905af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3906838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3907af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3908af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
3909ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
3910ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3911e97179c675b341927807c718be215c8d1aab8acbDouglas GregorTypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
3912e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  if (!ObjCInstanceTypeDecl)
3913e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    ObjCInstanceTypeDecl = TypedefDecl::Create(*this,
3914e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               getTranslationUnitDecl(),
3915e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
3916e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
3917e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               &Idents.get("instancetype"),
3918e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                     getTrivialTypeSourceInfo(getObjCIdType()));
3919e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return ObjCInstanceTypeDecl;
3920e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3921e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3922e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
3923e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
39242d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
3925e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
3926bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3927bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
39281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
392985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
393085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
393185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3932a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
393333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
39344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
3935f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  if (!type->isIncompleteArrayType() && type->isIncompleteType())
3936f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    return CharUnits::Zero();
3937f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
3938199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
39391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
394033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
39412ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
3942199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
394333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
394433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
3945199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
3946aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
3947199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
3948199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
3949199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
3950199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
3951199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
395233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
395333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
39546b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
39555e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
39566b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
39576b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
39586b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
39595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
39605e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
39615e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
39625e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
3963c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
39645e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
39655e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
39665e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
39675e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
3968199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3969199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
39706f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
39715e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
39725e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
3973aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3974199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
39755e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
39765e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
39775e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
3978199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
39795e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
39805e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
39815e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
39825e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
39835e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
39845e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
39855e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
39865e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
39875e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
39885e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
39895e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
39905e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
39915389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
39925389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
39935389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
39945389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
39955389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
39965389f48b24937ad7b4093307128b3cbf25235654David Chisnall    getObjCEncodingForType(PType, S);
39975389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
39985389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
39995389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
40006b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
40016b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
40025389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
40035389f48b24937ad7b4093307128b3cbf25235654David Chisnall
4004f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
40055389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
40065389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
40075389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
40085389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
40095389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
40105389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
40115389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
40125389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
40135389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
4014f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
4015f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
4016f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
40175389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
4018f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor        "getObjCEncodingForFunctionDecl - Incomplete param type");
40195389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
40205389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
40215389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
40225389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
40235389f48b24937ad7b4093307128b3cbf25235654David Chisnall
40245389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
40255389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
40265389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
40275389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
40285389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
40295389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
40305389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
40315389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
40325e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
40335e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
40345e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
40355e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
40365e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
40375e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
4038199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4039aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
40405e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
4041f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4042f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
40435e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
40445e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
4045a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
404633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
4047f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
40484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              std::string& S) const {
4049c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4050ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
4051a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
405233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
40530d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
405433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
405533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
405633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
405733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
4058199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
405933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
406033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
4061199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
4062491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
40637732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
406489951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
4065aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4066f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
4067f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
4068f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4069199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
4070199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
407133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
407233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4073199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
407433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
4075199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
40761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
407833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
4079491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
40807732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
4081491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis    const ParmVarDecl *PVDecl = *PI;
40821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
40834306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
4084ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4085ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
4086ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
4087bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
4088ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
4089ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
4090ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
4091ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
409233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
40934306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
40940d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
4095199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4096aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
409733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4098f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4099f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
410033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
410133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
4102c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
410383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
4104c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
4105c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
41061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
41071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
41081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
41091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
41101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
411183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
411283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
411383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
411483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
411583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
411683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
411783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
411883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
411983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
412083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
412183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
412283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
412383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
412483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
412583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
412683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
41271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
4128c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
41294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
4130c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
4131c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
4132c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
4133c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4134c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
4135c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
41361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
4137c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
4138c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
413917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
4140653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4141c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4142c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4143c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4144c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4145c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4146c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4147c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4148c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4149c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4150c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
415161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4152c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
415317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4154653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4155c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4156c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4157c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4158c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4159c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4160c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4161c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4162c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
41631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4164c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4165c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4166c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4167c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4168c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4169c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4170c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4171090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4172090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
41731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4174090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4175090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4176c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4177c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4178c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4179c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4180c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4181c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4182c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
41831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
41843a02b44e3948f7762dbfba94b7961281ca29d022Fariborz Jahanian    case ObjCPropertyDecl::Weak:   S += ",W"; break;
4185c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4186c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4187c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4188c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4189c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4190c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4191c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4192c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4193090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4194090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
41951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4196c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4197c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4198077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4199c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4200c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4201c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4202c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4203077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4204c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4205c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4206c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4207c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4208c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
420939f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4210c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4211c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4212c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4213c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4214c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4215a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
42161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
42171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4218a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4219a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4220a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
42218e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4222183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
42234ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4224a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
42251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
42264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
4227a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
4228a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4229a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
4230a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
4231a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
42327d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
42334ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
423482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
423582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
423682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
423782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
42381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
42395b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
424082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
424182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
424264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnallstatic char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
424364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    switch (T->getAs<BuiltinType>()->getKind()) {
4244b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unhandled builtin type kind");
424564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
424664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
424764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
424864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
424964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
425064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
425164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
42524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        return C->getIntWidth(T) == 32 ? 'L' : 'Q';
425364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
425464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
425564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
425664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
425764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
42583f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
42593f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
426064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
426164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
42624ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      return C->getIntWidth(T) == 32 ? 'l' : 'q';
426364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
426464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
426564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
426664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
42673a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
426864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
426964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
427064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
42715471bc85b69912e3b448de004498a80c0de32296Douglas Gregorstatic char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
42725471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  EnumDecl *Enum = ET->getDecl();
42735471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
42745471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of an non-fixed enum type is always 'i', regardless of size.
42755471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (!Enum->isFixed())
42765471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    return 'i';
42775471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
42785471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of a fixed enum type matches its fixed underlying type.
42795471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType());
42805471bc85b69912e3b448de004498a80c0de32296Douglas Gregor}
42815471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
42824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
428364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
4284a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl");
42858b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
428664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
428764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
428864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
428964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
429064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
429164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
429264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
429364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
429464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
429564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
429664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
429764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
429864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
429964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
430064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
430164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (!Ctx->getLangOptions().NeXTRuntime) {
430264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
430364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
430482905749d5c8d8b4edec11de754a73349cb96603Eli Friedman    S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
43055471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    if (const EnumType *ET = T->getAs<EnumType>())
43065471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(Ctx, ET);
4307c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    else
43084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      S += ObjCEncodingForPrimitiveKind(Ctx, T);
430964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
4310a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  S += llvm::utostr(FD->getBitWidthValue(*Ctx));
43118b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
43128b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
431301eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
431482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
431582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
431682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
4317153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
4318090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
43192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
43202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool StructField) const {
432164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (T->getAs<BuiltinType>()) {
4322ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
432364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
432464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(this, T);
4325ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4326ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
43271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4328183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
4329c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
43301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
4331c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
4332ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4333ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
433460bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
4335aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
4336aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
43376217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
43388d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
43398d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
43408d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
43418d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
4342aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
4343aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
4344aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4345aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
4346aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
4347a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
4348a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
4349a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
4350a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
43511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
43528e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
4353a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
4354a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4355a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4356a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
43579fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
4358a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
43596217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
43606217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
4361a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
4362a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4363a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4364a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
4365a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4366a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
4367a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
4368a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
4369a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
43705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (StringRef(S).endswith("nr"))
43710237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
4372a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
43731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
437485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
437585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
437685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
4377e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
437885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
437985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
438085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
43816217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
43829533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
43839533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
43849533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
43859533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
43869533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
43879533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
43889533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
43899533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
43909533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
43919533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
43929533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
439385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
439485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
4395a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
4396a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
43971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
439843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
4399ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4400ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4401aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
4402ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
4403ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
4404ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
44052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
4406559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
4407559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
4408559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
44091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4410559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4411559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
4412559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
44131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
44152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
44162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
44172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
44182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
44192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
4420559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
44212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
44222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
4423559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
4424559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
44251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4427559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4428559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
4429559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
4430ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4431ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4433183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
4434c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
4435ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4436ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44386217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
443982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
4440d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
4441502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
4442502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4443502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
44446fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
44456fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
44466fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
44476fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
44486fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
4449910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
4450910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
445130c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor                                            (*this).getPrintingPolicy());
44526fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
44536fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
44546fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
4455502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
4456502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
4457502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
44580d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
44597d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
44602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
44612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
44622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
44632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
44642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
44652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
44662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
44672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
44682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
44692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
44702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
44711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
44732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
44742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
44752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       (*Field));
44762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
44772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
44782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
44792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
44802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
44812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
44822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
44832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
4484d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
44857d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
44866de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
4487d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
4488ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4489ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4490e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
44915471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (const EnumType *ET = T->getAs<EnumType>()) {
44928b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
449364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      EncodeBitField(this, S, T, FD);
44948b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
44955471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(this, ET);
4496ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4497ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4499ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
450021a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
4501ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4502ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
45031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4504c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
4505c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4506c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
4507c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
45080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
450943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
45100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
451143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
451243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
451343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
451443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
4515db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    SmallVector<const ObjCIvarDecl*, 32> Ivars;
45162c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
45172c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4518db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
45192c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
45202c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
452143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
45222c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
452343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
452443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
4525ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
452643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
45271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4528183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
452914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
453014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
453114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4532ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
45331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
453427d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
453527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
453627d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
453727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
453814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
453914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4540ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
45411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4542ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
45431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
454414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
454514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
454614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
454714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
454814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
454914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
455067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
455167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
455214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
455314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
455414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
455514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
455614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
455714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
455814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4559ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
45601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4561ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
4562ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
4563ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
4564ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
45651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
4566ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
4567ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
45681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
45691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
4570ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
457114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
457214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
4573ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
4574ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
457527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
4576ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
457727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
457867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
457967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
4580ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
4581ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
4582ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
45831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4584ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
4585ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
4586ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4587ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
45881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4589532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
4590532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
4591532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
4592532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
4593e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4594e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  if (T->isVectorType()) {
4595e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
4596e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
4597e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
4598e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
4599e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
4600e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4601b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("@encode for type not implemented!");
460285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
460385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
46042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
46052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
46062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
46072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
46082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
46092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
46102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
46112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
46122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
46142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
46152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
46162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
46182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
46192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
46202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
46212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
46222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4623829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        if (base->isEmpty())
4624829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis          continue;
46252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        uint64_t offs = layout.getBaseClassOffsetInBits(base);
46262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
46272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
46282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
46292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
46302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
46312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
46332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
46342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
46352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
46362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
46372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
46382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, *Field));
46392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
46402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
46422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
46432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
46442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
46452636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4646829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      if (base->isEmpty())
4647829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        continue;
46482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t offs = layout.getVBaseClassOffsetInBits(base);
464919aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis      if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
465019aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
465119aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis                                  std::make_pair(offs, base));
46522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
46532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
46542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
46562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
46572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
46582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
46592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
46602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
46612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
46632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
46642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
46652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
4666cb8061e321bc6f46f154529885028256c7ae1fb2Argyrios Kyrtzidis  if ((CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) ||
4667cb8061e321bc6f46f154529885028256c7ae1fb2Argyrios Kyrtzidis      (CurLayObj == FieldOrBaseOffsets.end() &&
4668cb8061e321bc6f46f154529885028256c7ae1fb2Argyrios Kyrtzidis         CXXRec && CXXRec->isDynamicClass())) {
46692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CXXRec && CXXRec->isDynamicClass() &&
46702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           "Offset 0 was empty but no VTable ?");
46712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
46722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
46732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
46742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
46752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
46762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
46772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
46782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
46792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
46802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
46812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
46832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
46842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
46852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
46862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
46872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
46882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
46902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
46912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
46932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
46942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
46952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
46962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
46972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
46982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
46992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
47002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
47012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
47022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
47032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
47052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
47062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
47072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
47092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
47102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
47112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
47122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
47132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
4714829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      assert(!base->isEmpty());
4715829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
47162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
47172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
47182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
47192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
47202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
47212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
47222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
47232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
47252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
4726a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        CurOffs += field->getBitWidthValue(*this);
47272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
47282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
47292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
47302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
47312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
47322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
47332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
47342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
47352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
47362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
47372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
47382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
47392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
4741ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
4742ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
4743ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
4744ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
4745ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
4746ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
4747ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
4748ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
4749ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
4750ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
4751ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
4752ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
4753ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
4754ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
4755ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
4756ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
4757b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
47581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4759b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
4760b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
4761b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
47624dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas GregorTypedefDecl *ASTContext::getObjCIdDecl() const {
47634dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (!ObjCIdDecl) {
47644dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
47654dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    T = getObjCObjectPointerType(T);
47664dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
47674dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
47684dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     getTranslationUnitDecl(),
47694dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     SourceLocation(), SourceLocation(),
47704dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     &Idents.get("id"), IdInfo);
47714dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
47724dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
47734dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  return ObjCIdDecl;
47747e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
47757e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
47767a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas GregorTypedefDecl *ASTContext::getObjCSelDecl() const {
47777a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  if (!ObjCSelDecl) {
47787a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    QualType SelT = getPointerType(ObjCBuiltinSelTy);
47797a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT);
47807a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
47817a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      getTranslationUnitDecl(),
47827a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      SourceLocation(), SourceLocation(),
47837a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      &Idents.get("SEL"), SelInfo);
47847a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  }
47857a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  return ObjCSelDecl;
4786b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
4787b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
4788ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
4789a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
4790390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
4791390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
479279d6726921897811232554ed94c5d77b5b7b3fc0Douglas GregorTypedefDecl *ASTContext::getObjCClassDecl() const {
479379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  if (!ObjCClassDecl) {
479479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
479579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    T = getObjCObjectPointerType(T);
479679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
479779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
479879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        getTranslationUnitDecl(),
479979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        SourceLocation(), SourceLocation(),
480079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        &Idents.get("Class"), ClassInfo);
480179d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  }
480279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
480379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  return ObjCClassDecl;
48048baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
48058baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
4806a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
48071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
48082198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
48091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4810a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
48112198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
48122198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
48130bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
48140bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
48154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
48164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
48174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
48180bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
48190bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
48207532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
48210bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
48220bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
48230bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
48240bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
48250bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
4826eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
48270bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
48280bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
48290bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
48300bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
48310bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
48327532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
48337532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
48340bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
48357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
48367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
4837d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
4838d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
48394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
48404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
48414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
48424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
48430f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
48440f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
4845789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
4846d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
4847d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
48481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4849d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
4850d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
48510bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4852d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
4853d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4854d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4855d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
48561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4857d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
4858d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
4859d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
48607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
48617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
48624ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
48634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
48644ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
48651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
48663b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
48677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
48687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
48697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
48707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
48717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
48727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
48737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
48747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
48757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
48767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
48777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
48787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
48797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
48807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
48817532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
48827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
48837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
4884789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
4885789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4886789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
4887789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
48887532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
48897532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
48907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
48917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
48927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
48937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
4894ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
4895ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
4896ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
4897ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
48984ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
4899ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
4900ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
4901ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4902ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
4903ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
4904ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4905ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
4906789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
4907789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4908ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4909ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
4910ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
4911ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4912ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4913ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
4914ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4915ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
4916ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4917ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
4918789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
4919789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
4920789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4921789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
4922789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
4923ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
4924ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4925ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
4926ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
4927ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
4928ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
49291aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
4930146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
4931146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                         TemplateName replacement) const {
4932146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm::FoldingSetNodeID ID;
4933146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
4934146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4935146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  void *insertPos = 0;
4936146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage *subst
4937146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
4938146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4939146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  if (!subst) {
4940146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
4941146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParms.InsertNode(subst, insertPos);
4942146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
4943146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4944146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  return TemplateName(subst);
4945146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall}
4946146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4947146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallTemplateName
49481aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
49491aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
49501aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
49511aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
49521aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
49531aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
49541aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
49551aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
49561aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
49571aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
49581aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
4959146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
49601aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
49611aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
49621aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
49631aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
49641aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
49651aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
49661aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
49671aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
4968b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
4969d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
4970d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
4971e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
4972b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
4973e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
4974b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
4975b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
4976b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
4977b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
4978b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
4979b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
4980b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
4981b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4982b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
4983b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
4984b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled TargetInfo::IntType value");
4985b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
4986b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
4987b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
4988b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
4989b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
4990b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
49914fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
49924fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
49934fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
4994ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
4995e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor  if (getLangOptions().getGC() == LangOptions::NonGC)
4996ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
4997ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
4998ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  assert(getLangOptions().ObjC1);
4999ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
5000ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
5001ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
5002ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
5003ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
5004ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
5005ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
5006ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
5007ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
5008ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
5009ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
5010ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
5011ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
5012ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
5013ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
5014ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
5015ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
5016ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
5017ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
50184fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
5019b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
50204fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
50214fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
50226ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
50236ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
50246ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
5025770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
50261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
50276ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
50286ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
50296ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
5030467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
50316ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
503261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
50336ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
50346ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5035255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
5036255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
5037255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
5038255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
5039255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5040255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
5041255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5042255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5043f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
5044f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
5045255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
5046255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
5047f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
5048255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
5049f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
5050f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
5051f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
5052f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
5053255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5054255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5055255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
5056255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
5057255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
50584084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
50594084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
50604084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
50614084c306635b70f37029dca938444e6013f08684Steve Naroff
50624084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
50634084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
50644ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
50654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
50664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
50674084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
506814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
50694084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
50704084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
50714084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
50724084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
50734084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
50744084c306635b70f37029dca938444e6013f08684Steve Naroff}
507514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
50764084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
50774084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
50784084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
50794084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
50804084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
50814084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
50824084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
50834084c306635b70f37029dca938444e6013f08684Steve Naroff}
50844084c306635b70f37029dca938444e6013f08684Steve Naroff
5085a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// ObjCQualifiedClassTypesAreCompatible - compare  Class<p,...> and
5086a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// Class<p1, ...>.
5087a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
5088a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
5089a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
5090a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
5091a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
5092a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5093a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
5094a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
5095a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
5096a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
5097a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
5098a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
5099a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
5100a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
5101a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
5102a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
5103a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
5104a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
5105a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
5106a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
5107a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
5108a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
5109a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
5110a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
51114084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
51124084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
51134084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
51144084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
51154084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
51161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
51174084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
51184084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
51191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
51204084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
51214084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
51224084c306635b70f37029dca938444e6013f08684Steve Naroff
51234084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
5124183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
51251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51264084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
51271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51284084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
51291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
51304084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
51314084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
51324084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
51334084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
51344084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
51354084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
51364084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
51370fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
51384084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
51394084c306635b70f37029dca938444e6013f08684Steve Naroff        }
51404084c306635b70f37029dca938444e6013f08684Steve Naroff      }
51414084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
51424084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
51434084c306635b70f37029dca938444e6013f08684Steve Naroff    }
51441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
51454084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
51464084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
51474084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
51484084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
5149de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
5150de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
5151de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
5152de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
51534084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
51544084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
51554084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
51564084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
51574084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
51584084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
51598f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
51608f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
5161de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
51621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
51634084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
51644084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
51654084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
51664084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
51674084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
51684084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
51694084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
51700fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
51714084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
51724084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
51734084c306635b70f37029dca938444e6013f08684Steve Naroff          }
51744084c306635b70f37029dca938444e6013f08684Steve Naroff        }
51754084c306635b70f37029dca938444e6013f08684Steve Naroff      }
51764084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
5177de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
5178de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
51791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5180de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
5181de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
51821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51834084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
51844084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
51854084c306635b70f37029dca938444e6013f08684Steve Naroff
51861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
51874084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
5188de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
51894084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
51904084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
51914084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
51924084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
51934084c306635b70f37029dca938444e6013f08684Steve Naroff
5194de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
51954084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
51964084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
5197de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
5198de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
51994084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
52004084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
52014084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
52024084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
52034084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
52044084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
52054084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
52064084c306635b70f37029dca938444e6013f08684Steve Naroff        }
52074084c306635b70f37029dca938444e6013f08684Steve Naroff      }
52084084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
52094084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
52104084c306635b70f37029dca938444e6013f08684Steve Naroff    }
5211de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
5212de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
5213de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
5214de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
5215de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5216de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
5217de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
5218de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
5219de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
5220de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
5221de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
5222de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5223de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
5224de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
5225de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
5226de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
5227de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
5228de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
5229de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
5230de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
5231de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
5232de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
5233de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
5234de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
5235de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
5236de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
5237de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
5238de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
5239de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
52404084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
52414084c306635b70f37029dca938444e6013f08684Steve Naroff  }
52424084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
52434084c306635b70f37029dca938444e6013f08684Steve Naroff}
52444084c306635b70f37029dca938444e6013f08684Steve Naroff
52454084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
52464084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
52474084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
52484084c306635b70f37029dca938444e6013f08684Steve Naroff///
52494084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
52504084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
5251c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5252c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5253c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
52544084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
5255c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
5256c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
52574084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
52584084c306635b70f37029dca938444e6013f08684Steve Naroff
5259c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
52601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
52611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
52624084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
5263a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5264a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
5265a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
5266a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
5267a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5268c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
5269c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
52704084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
52711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52724084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
527314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
527414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5275132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
5276fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
5277132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
5278132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
5279132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
5280132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
5281132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
5282a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
5283a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
5284a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
5285132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
5286132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5287132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
5288132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
5289132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5290132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5291a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
5292132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
5293132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
5294132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
5295132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5296132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
5297132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
5298132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
5299132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
5300132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
5301a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
5302132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
5303a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
5304132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5305132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
5306132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
5307132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5308132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
5309132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5310132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5311e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
5312e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
5313e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
5314e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
5315e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
5316e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
5317e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
5318e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
53195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
5320e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5321c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5322c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5323c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
5324c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
5325e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5326e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
5327e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
5328e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
5329e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
5330e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5331432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5332c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
5333c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
5334e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
5335e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
5336e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5337e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5338e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
5339e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
5340cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
5341cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
5342e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
5343e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
5344e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
53453060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
5346432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
5347c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
5348c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
5349432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5350432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
5351432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
5352432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
5353432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
5354e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5355e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
5356e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5357db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
5358db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
5359db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
5360db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
5361db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
5362c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
5363c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
5364c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
5365c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
5366c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
5367c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
53687c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  if (!LDecl || !RDecl || (LDecl == RDecl))
5369db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
5370db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
53717c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
5372c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
5373e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
53745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl *, 8> Protocols;
5375c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
5376c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5377c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
5378c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
5379c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
5380c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
5381c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
5382e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
53837c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
5384db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5385db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
5386db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
5387db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5388c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5389c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
5390c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
5391c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
5392c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
53936ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
53946ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
5395c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
53966ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
53971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53986ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
53996ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
5400c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
54016ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
54021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5403b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
5404b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
5405b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
5406b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
5407b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
5408b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
5409627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
5410627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5411627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
5412b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
5413b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
5414b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5415b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5416b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
5417627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
5418b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
5419b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
5420b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
5421b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5422b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5423b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
5424b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
5425b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
5426b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
5427b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5428b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5429b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
5430b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5431b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
5432b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5433b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
5434b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
5435b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
5436b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
5437b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
5438b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
5439b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
5440b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
5441b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
5442b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
5443b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
54441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5445c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5446c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
544791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
544891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
544991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
545091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
545191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
5452c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5453c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
54548f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
54558f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
545691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
54578f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
54588f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
545991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
546091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
546191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
546291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
546391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
546491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
546591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
54666ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
54676ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5468389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5469389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
5470183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5471183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
54721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
547314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
5474389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
547514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
547614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
547714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
5478389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
5479389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
5480569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5481569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
5482569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5483569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5484569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
5485569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
54861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
5487ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
54881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
5489ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
5490447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5491447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
54920e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  if (getLangOptions().CPlusPlus)
54930e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
54940e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
5495447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
54963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
54973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5498c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanianbool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
549982378391dfc3b2fc22f63dbf1552bd9aed39dc42Fariborz Jahanian  return typesAreCompatible(LHS, RHS);
5500c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian}
5501c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian
5502132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5503132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
5504132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5505132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
55064846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
55074846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
55084846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
55094846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
55104846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
55114846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
55124846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
55134846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
55144846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
55154846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
55164846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
5517f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
55184846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
55194846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
55204846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
55214846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
55224846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
55234846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
55244846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
55254846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
55264846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
55274846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
55284846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
55294846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
55304846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
55314846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
55324846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
55334846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
55344846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
55354846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
55364846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
55374846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
55384846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
55394846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
55404846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
55414846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
55424846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
55434846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
55444846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
55454846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
55464846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
55474846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
55484846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
5549132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
5550447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
5551447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
5552183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
5553183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
555472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
555572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
55563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
55573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
55583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
5560132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
5561d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
5562d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
5563d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
5564d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
5565d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
5566d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
5567a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
5568d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
5569132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
55708cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
55718cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
55723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
5573447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5574447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
5575447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
5576447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5577447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
5578447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
5579447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5580447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
5581447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
5582447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
5583447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5584447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
558561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
5586447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
558761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
55888cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
55896a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
55906a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
55916a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
55926a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
5593264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5594264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
55958cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
55968cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
55978cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
55988cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
55998cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
56008cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
5601a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5602a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
56038cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
56048cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
56058cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
5606f85e193739c953358c865005855253af4f68a497John McCall  if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
5607f85e193739c953358c865005855253af4f68a497John McCall    return QualType();
5608f85e193739c953358c865005855253af4f68a497John McCall
560953c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // functypes which return are preferred over those that do not.
561053c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn())
56116a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
561253c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn())
56136a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
561453c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
561553c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
56161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5617f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
5618e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
56193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
5620465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5621465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
56223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
56233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
56243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
56263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
56273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
56283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
56303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
56313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
56323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56337fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
56347fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
56357fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
563678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    if (LangOpts.ObjCAutoRefCount &&
563778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto))
563878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      return QualType();
563978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
56403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
56415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 10> types;
56423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
56433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
56443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
56454846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
56464846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
56474846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
56483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
5649447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5650447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
5651447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
5652447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
56533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
5654447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
5655447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
5656447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
5657447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
5658447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
565961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
566061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
566161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
566261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
56633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
566478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
56653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
56663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5667e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5668e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5669e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
5670e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    return getFunctionType(retType, types.begin(), types.size(), EPI);
56713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
56723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
56743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
56753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
567672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
56773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
5678465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
56793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
56803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
56813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
56823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
56833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
56843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
56853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
56863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
56873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
5688b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
5689b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
5690b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
5691b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
5692b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
5693b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
56943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
56953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
56963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
56973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
56983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
57003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5701e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5702e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5703e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
57043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
5705e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                           proto->getNumArgs(), EPI);
57063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
57073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
57083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
57093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
57108cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
57113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
57123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5713132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
5714447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
5715a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
571643d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
571743d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
571843d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
57197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
57207c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
57210e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
57220e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
5723447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5724447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5725447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
5726447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
5727447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
57280e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
57293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
57303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
57313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5732f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
57333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
57343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
57353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
57360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
5737a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
5738a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
57390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
57400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
57410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
57420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5743f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
5744f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
57450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
57460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
57470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
57480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
57490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
57500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
57510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
57520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
57530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
57540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
57550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
57560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
57570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
57580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
57590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
57600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
57610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
57620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
57630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
57640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
57653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
57660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
57670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
57680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
57693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5770852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
5771852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
5772f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
57731adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
57741adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
57751adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
57761adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
57774c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
57784c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
5779a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5780a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
5781a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5782a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
57831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5784c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
5785c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5786c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5787c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5788213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
5789213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5790213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
57911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57924e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
57934e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
57941adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
57951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
5796842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
5797842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
5798183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
57993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
58003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
5801bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
5802183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
58033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
58043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
5805bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
58061adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
58073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
5808ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
58093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
58104a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
58111adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
581272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
581372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
5814ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
581572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
581672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
581772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
5818b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Non-canonical and dependent types shouldn't get here");
581972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
58207c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
58217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
582272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
5823b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("C++ should never be in mergeTypes");
582472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5825c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
582672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
582772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
582872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
582972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
5830b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Types are eliminated above");
583172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
58321adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
58333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
58343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
58356217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
58366217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
5837447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5838447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5839447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5840447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5841447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5842447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
58433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
584407d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
584561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
584607d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
584761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
58483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
58493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
5850c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
5851c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
5852c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
58536217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
58546217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
5855447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5856447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5857447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5858447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5859447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5860447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
5861c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
5862c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5863c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
5864c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5865c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
5866c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
5867c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
5868b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
5869b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  {
5870b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Merge two pointer types, while trying to preserve typedef info
5871b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType LHSValue = LHS->getAs<AtomicType>()->getValueType();
5872b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType RHSValue = RHS->getAs<AtomicType>()->getValueType();
5873b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Unqualified) {
5874b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      LHSValue = LHSValue.getUnqualifiedType();
5875b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      RHSValue = RHSValue.getUnqualifiedType();
5876b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
5877b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
5878b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                     Unqualified);
5879b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (ResultType.isNull()) return QualType();
5880b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
5881b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return LHS;
5882b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
5883b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return RHS;
5884b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return getAtomicType(ResultType);
5885b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
58861adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
58873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
58883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
58893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
58903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
58913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
58923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
58933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
58943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
5895447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5896447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
5897447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
5898447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5899447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5900447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
59013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
590261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
590361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
590461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
590561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
59063bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
59073bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
59083bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
59093bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
59103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
59113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
591261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
591361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
591461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
591561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
59163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
59173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
59183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
59193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
59203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
59213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
59223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
59233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
59243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
59253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
59263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
59273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
59283bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
59293bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
59307e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
59317e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
59323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
59331adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
5934447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
593572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
593672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
59373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
59381adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
59393cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
59403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
594164cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
594264cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
594364cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
59443cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
59455a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
59461c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
59471c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
59483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
594961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
5950c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
5951c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
59525a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
59535a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
5954c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5955c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5956c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
59575fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
59585fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
59593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
596061490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
596114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
5962132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
5963132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
5964132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
5965a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
5966a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
5967132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return LHS;
5968132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
5969132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5970183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5971183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
597214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
597314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5974bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
5975132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5976ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
597772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
597872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
5979ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
59807192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
598178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanianbool ASTContext::FunctionTypesMatchOnNSConsumedAttrs(
598278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *FromFunctionType,
598378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *ToFunctionType) {
598478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromFunctionType->hasAnyConsumedArgs() !=
598578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      ToFunctionType->hasAnyConsumedArgs())
598678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    return false;
598778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo FromEPI =
598878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    FromFunctionType->getExtProtoInfo();
598978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo ToEPI =
599078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    ToFunctionType->getExtProtoInfo();
599178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments)
599278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
599378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian         ArgIdx != NumArgs; ++ArgIdx)  {
599478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      if (FromEPI.ConsumedArguments[ArgIdx] !=
599578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian          ToEPI.ConsumedArguments[ArgIdx])
599678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        return false;
599778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    }
599878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  return true;
599978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian}
600078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
60012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
60022390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
60032390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
60042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
60052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
60062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
60072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
60082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
60092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
60102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
60112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
60122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
60132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
60142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
60152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
60162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
60172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
60182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
60192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
60202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
60212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
60222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
60232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
60242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
60252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
6026e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
6027e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
60282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
60292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
6030e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                            FPT->getNumArgs(), EPI);
60312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
60322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
60332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
60342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
60352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
60362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
60372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
60382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
60392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
60402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
60412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
60422390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
60432390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
60442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
60452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
60462390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
60472390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
60482390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
60492390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
60502390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
60512390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
60522390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
60532390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
60542390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
60552390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
60562390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
60572390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
60582390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
60592390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
60602390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
60612390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
60622390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
60632390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
60642390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
60652390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
60662390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
60672390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
60682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
60692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
60702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
60712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
60722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
60732390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
60742390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
60752390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
60762390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
60775426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
6078ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
6079ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
608088054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
60814ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
6082f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
608329a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
60841274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
60851274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
6086f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
6087ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
6088ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6089ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
6090ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
6091f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
60926a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
60936a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
60946a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
60956a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
6096e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
60976a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
60986a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
60996a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
6100ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
61016a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
61026a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
61036a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
6104ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
6105ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
6106ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
6107ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
6108ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
6109ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
6110ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
6111ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
6112ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
6113ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
6114ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
6115ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
61162df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
61172df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
6118ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
6119b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Unexpected signed integer type");
6120ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
6121ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6122ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
61237b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
61247b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
612586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
612686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
612786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
612886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
612986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
613086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
613133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
613233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
613333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
613433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
613514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
613614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
613714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
61384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
613986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
614014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
614133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
614286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
614386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
614486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
614514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
6146393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
614733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
614886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
614986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
615086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
61511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
6152393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
615314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
6154393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
615586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
615686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
615786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
615886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
615986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
616086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
616186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
616286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
616386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
616486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
616586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
616686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
616786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
616886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
616986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
617086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
617186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
617286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
61731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
617486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
617586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
6176b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown builtin type letter!");
617786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
617886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
617986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
618086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
618186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
618286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
618386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
618486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
618586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
618686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
618786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
618886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
618986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
619086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
619186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
619286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
619386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
619486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
619586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
619686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
619786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
619886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
619986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
620086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
620186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
620286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
620386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
620486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
620586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
620686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
620786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
620886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
620986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
621086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
621186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
621286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
621386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
621486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
621586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
621686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
621786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
621886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
621986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
622086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
622186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
622286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
622386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
622486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
622586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
622686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
622786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
622886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
622986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
623086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
623186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
6232ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
6233ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
6234ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
6235ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
6236ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
6237ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
623886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
623986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
624086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
624186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
624286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
624386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
624486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
624586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
624686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
624786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
624886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
624986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
625086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
625186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
625286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
625314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
625486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
625514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
625686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
625786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
625886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
625986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
626086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
626186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
626286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
62631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
626414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
626514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
626614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
626733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
626833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
6269788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
6270e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
627186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
627286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6273d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
627414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
627514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
627614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
6277d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
6278d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
6279cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
6280cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  case 'Y' : {
6281cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    Type = Context.getPointerDiffType();
6282cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    break;
6283cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
62849a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
6285c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
6286c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
6287f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
628886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
628986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
6290fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
62919a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
6292f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
6293782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
6294f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
6295f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
6296f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
6297fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
6298f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
6299fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
6300fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
6301fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
6302782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
63031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
630433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
630533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
630686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
6307187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
630833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
630933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
631033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
631133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
631233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
631333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
631433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
631533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
631633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
631733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
631833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
631933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
632033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
632133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
632233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
632333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
632433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
632533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
632633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
632733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
632833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
632933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
633033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
633133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
633286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
633386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6334393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
633514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
6336393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
63371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
633886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
633986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
634086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
634186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
634233daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
634314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
63444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
634533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
63461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 8> ArgTypes;
63481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
634914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
635086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
635114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
635214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
635386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
635486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
635514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
635614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
635714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
635886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
635914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
636086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
636186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
636286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
636314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
636414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
636514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
636614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
636714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
636886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
636986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
637086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
63711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
637286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
637386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
637486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
637586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
637686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
637786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
637800ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
637900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
638000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
638100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
638200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
638300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
638400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
638500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
6386ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
6387e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
638800ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
638900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
6390e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6391e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
639286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
6393a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
639490e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
639590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage External = GVA_StrongExternal;
639690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
639790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = FD->getLinkage();
639890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
639990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
640090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
640190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
640290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
640390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
640490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
640590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (FD->getTemplateSpecializationKind()) {
640690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
640790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
640890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_StrongExternal;
640990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
641090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
641190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
641290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
641390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
641490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
641590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
641690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_TemplateInstantiation;
641790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
641890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
641990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
642090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
642190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
642290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
642390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
642490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
642590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
642690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
642790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
642890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
642990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
643090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
643190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
643290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
643390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
643490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
643590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
643690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
643790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
643890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
643990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
644090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
644190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
644290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
644390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
644490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
644590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
644690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
644790e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
644890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
644990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
645090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
645190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
645290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
645390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
645490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
645590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = VD->getLinkage();
645690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
645790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      VD->getType()->getLinkage() == UniqueExternalLinkage)
645890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    L = UniqueExternalLinkage;
645990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
646090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
646190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
646290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
646390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
646490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
646590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
646690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
646790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (TSK) {
646890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
646990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
647090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_StrongExternal;
647190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
647290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
647390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      llvm_unreachable("Variable should not be instantiated");
647490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      // Fall through to treat this like any other instantiation.
647590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
647690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
647790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
647890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
647990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
648090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_TemplateInstantiation;
648190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
648290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
648390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
648490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_StrongExternal;
648590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
648690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
64874ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
648890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
648990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
649090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
6491144b38a7995cbe0928e34fbcc865bb2d2be4f7a3Argyrios Kyrtzidis  } else if (!isa<FunctionDecl>(D))
649290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
649390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6494ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
6495ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
6496ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6497ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
649890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
649990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
650090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
650190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
650290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
650390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
650410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
6505dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      return FD->doesDeclarationForceExternallyVisibleDefinition();
650690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
650790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
650890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
650990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
651090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
651190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // The key function for a class is required.
651290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
651390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      const CXXRecordDecl *RD = MD->getParent();
651490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      if (MD->isOutOfLine() && RD->isDynamicClass()) {
651590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
651690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
651790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis          return true;
651890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
651990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
652090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
652190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
652290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
652390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
652490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
652590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
652690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
652790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
652890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
652990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
653090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
653194da1587f7d584fc61df793229d197969f204cd9Douglas Gregor
653290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
653390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
653490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6535ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6536ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6537ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
653890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Structs that have non-trivial constructors or destructors are required.
653990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
654090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // FIXME: Handle references.
6541023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  // FIXME: Be more selective about which constructors we care about.
654290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
654390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
6544023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() &&
6545023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialCopyConstructor() &&
6546023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialMoveConstructor() &&
6547023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialDestructor()))
654890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        return true;
654990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
655090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
655190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
655290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
655390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
655490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
655590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
655690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
655790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
655890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return true;
655990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
6560071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
6561ee743f903858e337434ac0335f147f4de4ecae05Charles DavisCallingConv ASTContext::getDefaultMethodCallConv() {
6562ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
6563ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  return ABI->getDefaultMethodCallConv();
6564ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
6565ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
65664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
6567dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
6568dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
6569dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
6570dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
657114110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
6572bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  switch (Target->getCXXABI()) {
657314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_ARM:
657414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Itanium:
657514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
657614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Microsoft:
657714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
657814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
6579b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unsupported ABI");
658014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
658114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
6582071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
6583ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
6584ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
65850c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek  return ASTRecordLayouts.getMemorySize()
65860c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCLayouts)
65870c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(KeyFunctions)
65880c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCImpls)
65890c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(BlockVarCopyInits)
65900c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(DeclAttrs)
65910c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember)
65920c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingDecl)
65930c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl)
65940c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl)
65950c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(OverriddenMethods)
65960c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(Types)
6597af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(VariableArrayTypes)
65980d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    + llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
6599ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
6600d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
6601d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
6602d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParamIndices[D] = index;
6603d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
6604d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
6605d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
6606d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParameterIndexTable::const_iterator I = ParamIndices.find(D);
6607d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  assert(I != ParamIndices.end() &&
6608d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek         "ParmIndices lacks entry set by ParmVarDecl");
6609d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return I->second;
6610d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
6611